"Java LeetCode Challenge: Summary Ranges Problem"

"🚀 Day 44 of 50 – Java LeetCode Challenge" Today’s challenge was “228. Summary Ranges” — a neat array-based problem that enhances your understanding of iteration, range detection, and string manipulation in Java. The goal is to summarize continuous sequences of numbers into compact range strings. ⏱ Today’s Task – Summary Ranges 📝 Problem Statement: You are given a sorted unique integer array nums. A range [a, b] is defined as all integers from a to b (inclusive). Return the smallest sorted list of ranges that covers all numbers in the array exactly. Each range should be represented as: "a->b" if a != b "a" if a == b 🧪 Examples: Input: nums = [0,1,2,4,5,7] Output: ["0->2","4->5","7"] ✅ Input: nums = [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] ✅ 🔧 Constraints: 0 <= nums.length <= 20 -2³¹ <= nums[i] <= 2³¹ - 1 All values are unique and sorted in ascending order 💻 My Java Solution: import java.util.*; class Solution {   public List<String> summaryRanges(int[] nums) {     List<String> result = new ArrayList<>();     if (nums.length == 0) return result;     int start = nums[0];     for (int i = 1; i < nums.length; i++) {       if (nums[i] != nums[i - 1] + 1) {         if (start == nums[i - 1]) {           result.add(String.valueOf(start));         } else {           result.add(start + "->" + nums[i - 1]);         }         start = nums[i];       }     }     if (start == nums[nums.length - 1]) {       result.add(String.valueOf(start));     } else {       result.add(start + "->" + nums[nums.length - 1]);     }     return result;   } } 🔍 Takeaways: ✅ Efficient handling of continuous integer sequences ✅ Smart use of loop tracking and condition checks ✅ Clean, readable implementation with O(n) time complexity ✅ Handles edge cases gracefully (like empty arrays or single elements) 💡 Core Concepts: 📘 Array traversal and pattern detection 📘 String concatenation and list building 📘 Conditional logic for sequence grouping 🎯 Day 44 completed — 6 more to go! 🚀 #Java #50DaysOfCode #LeetCode #ProblemSolving #Arrays #DataStructures #JavaProgramming #Day44 #CodeNewbie #LearnInPublic

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories