easy to follow java solution


  • 1
    F
        public List<String> summaryRanges(int[] nums) {
            ArrayList<String> result = new ArrayList<>();
            if (nums == null || nums.length == 0) {
                return result;
            }
            int n = nums.length;
            int begin = 0, end = 0;
            while (end < n) {
                end++;
                while (end < n && nums[end - 1] + 1 == nums[end]) {
                    end++;
                }
                int count = end - begin;
                if (count == 1) {
                    result.add(String.valueOf(nums[begin]));
                } else {
                    result.add(nums[begin] + "->" + nums[end - 1]);
                }
                begin = end;
            }
            return result;
        }

Log in to reply
 

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