0ms Java simple code, checking 9 in while loop


  • 3
    Y
    public class Solution {
        public int[] plusOne(int[] digits) {
            // we may need to create a digits.length+1 array
            int currInd = digits.length-1;
            while (currInd >= 0 && digits[currInd] == 9) {
                digits[currInd--] = 0;
            }
            if (currInd >= 0) {
                digits[currInd]++;
                return digits;
            }
            int[] newArray = new int[digits.length+1];
            newArray[0] = 1;
            return newArray;
        }
    }

  • 0
    S

    public class Solution {
    public int[] plusOne(int[] nums) {
    for(int i=nums.length-1;i>=0;i--){
    nums[i]++;
    if(nums[i]<10) break;
    if(i!=0) nums[i]=0;
    }
    if(nums[0]==10){
    int[] a=new int[nums.length+1];
    a[0]=1;
    return a;
    }
    return nums;
    }
    }


Log in to reply
 

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