JAVA recursion solution


  • 11
    S
    public class Solution {
        public int[] plusOne(int[] digits) {
            return helper(digits,digits.length-1);
        }
        
        private int[] helper(int[] digits, int index){
            if(digits[index] < 9){
                digits[index]++;
                return digits;
            }else{
                if(index != 0){
                    digits[index] = 0;
                    return helper(digits,index-1);
                }else{
                    int[] res = new int[digits.length+1];
                    res[0] = 1;
                    return res;
                }
            }
        }
    }

  • 0
    S

    Interesting recursion. U must have a good understanding about the problem : ).


  • 0

    As for me, it's a little complicated, but you do well.


Log in to reply
 

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