easy java solution


  • 0
    Z
    public int[] plusOne(int[] digits) {
            int i = digits.length - 1;
            digits[i] += 1;
            while(digits[i] == 10 && i >= 1){
                digits[i] = 0;
                digits[i-1] += 1;
                i--;
            }
            if(digits[0] == 10){
                digits[0] = 0;
                int[] res = new int[digits.length+1];
                res[0] = 1;
                for(i=1;i<res.length;i++){
                    res[i] = digits[i-1];
                }
                return res;
            }else{
                return digits;
            }
        }
    

Log in to reply
 

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