AC JAVA solution, 0ms and easy to uns


  • 1
    public class Solution {
        public List<String> summaryRanges(int[] nums) {
            List<String> res = new ArrayList<String>();
            int len = nums.length;
            if (len == 0)
                return res;
            int r=0; // runner to record the current start point
            for (int i=1; i<len; i++){
                if (nums[i] != nums[i-1]+1){ // if a gap is found
                    if (i-r == 1)
                        res.add(nums[r]+"");
                    else
                        res.add(nums[r]+"->"+nums[i-1]);
                    r = i;  // the runner will move forward to jump accross the gap
                }
            }
            if (len - r == 1)
                res.add(nums[r]+"");
            else
                res.add(nums[r]+"->"+nums[len-1]);
            return res;
        }
    }
    

  • 0
    L

    Good Answer. Very easy to understand.


Log in to reply
 

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