two pointers, easy to understand


  • 0
    C
    public class Solution {
        public List<String> summaryRanges(int[] nums) {
            List<String> list = new ArrayList<String>();
            int len = nums.length;
            if(len==0){
                return list;
            }
            int i = 0,j=1;
            while(j<len){
                if(nums[j]-nums[j-1]!=1){
                    if(j-i>1){
                        list.add(nums[i]+"->"+nums[j-1]);
                        i=j;
                    }else{
                        list.add(nums[i]+"");
                        i++;
                    }
                }
                j++;
            }
            if(i==len-1){
                list.add(nums[i]+"");
            }else{
                list.add(nums[i]+"->"+nums[len-1]);
            }
            return list;
        }
    }
    

Log in to reply
 

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