My Java Recursive Solution


  • 0
    L

    I use a very concise way to solve this solution, here is my code

    public class Solution {
        List<String> res = new LinkedList<>();
        public List<String> summaryRanges(int[] nums) {
            summaryRangeHelper(nums);
            return res;
        }
        
        private void summaryRangeHelper(int[] nums) {
            if (nums.length == 0) return;
            if (nums.length == 1) {
                res.add("" + nums[0]);
                return;
            }
            String range = "" + nums[0];
            int i = 1;
            for ( ; i < nums.length && nums[i] - nums[i - 1] == 1; i++);
            if (i > 1) {
                range += "->" + nums[i - 1];
            }
            res.add(range);
            summaryRangeHelper(Arrays.copyOfRange(nums, i, nums.length));
        }
    }
    

Log in to reply
 

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