0ms Java solution


  • 0
    T
    public List<String> summaryRanges(int[] nums) {
            LinkedList<String> ret = new LinkedList<>();
            if (nums == null || nums.length == 0) return ret;
            int begin = 0;
            int end = 0;
            for (int i = 0; i < nums.length - 1; i++) {
                if (nums[i + 1] - nums[i] == 1) {
                    end++;
                } else {
                    String s = begin != end ? nums[begin] + "->" + nums[end] : nums[begin] + "";
                    begin = end + 1;
                    end = end + 1;
                    ret.add(s);
                }
            }
            String s = begin != end ? nums[begin] + "->" + nums[end] : nums[begin] + "";
            ret.add(s);
            return ret;
        }
    

Log in to reply
 

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