My Java Solution


  • 0
    D
    public class Solution {
        public List<String> summaryRanges(int[] nums) {
            int len = nums.length;
            List<String> list = new ArrayList<>();
            if (len == 0) return list;
            int start = nums[0];
            int end = nums[len-1];
            String s="";
            for (int i=0; i<len-1; i++) {
                if (nums[i+1]== (nums[i]+1)) continue;
                else {
                    end = nums[i]; //set end to current int
                    if (start<end) 
                        s = String.valueOf(start) + "->" + String.valueOf(end);
                    else
                        s = String.valueOf(start);
                    list.add(s);
                    start = nums[i+1]; //reset start & end
                    end = nums[len-1];
                }
            }
            if (start<end)
                s = String.valueOf(start) + "->" + String.valueOf(end);
            else
                s = String.valueOf(start);
            list.add(s);
            return list;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.