My Java solution


  • 0
    R

    public class Solution {
    public List<String> summaryRanges(int[] nums) {
    int start = 0;
    List<String> res = new ArrayList<String>();
    while(start < nums.length){
    int count = 1;
    while(start + count < nums.length && nums[start + count] - nums[start] == count) count++;
    if(count <= 1) res.add(nums[start] + "");
    else res.add(nums[start] + "->"+ nums[start + count - 1]);
    start += count;
    }
    return res;
    }
    }


Log in to reply
 

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