java recursive solution


  • 0
    P

    I still don't know why I used recursion on this problem......
    public int[] plusOne(int[] digits) {
    int num = digits.length-1;
    return recur(digits,num);
    }
    public int[] recur(int[] digits,int num)
    {
    if(digits[num]<9)
    {
    digits[num]++;
    return digits;
    }
    if(digits[num] == 9)
    {
    digits[num] = 0;
    }
    if(num == 0)
    {
    int[] newdi = new int[digits.length+1];
    for(int i = 1;i<newdi.length;i++)
    {
    newdi[i] = digits[i-1];
    }
    newdi[0] = 1;
    return newdi;
    }
    return recur(digits,--num);
    }


Log in to reply
 

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