Simple java solution


  • 0
    J
        public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if(nums==null || nums.length==0) return res;
        int pre = nums[0];
        String str = pre+"";
        int count=1;
        for(int i=1;i<nums.length;i++){
            int cur = nums[i];
            if(cur - pre==1){
               count++;
            }else{
                if(count>1) str = str + "->"+pre;
                res.add(str);
                str = cur+"";
                count =1;
            }
              pre = cur;
        }
        if(count>1) str = str + "->"+pre;
        res.add(str);
        return res;
    }

Log in to reply
 

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