Very simple Java solution with O(N) runtime.


  • 2
    A
    public int[] plusOne(int[] digits) {
    
        int carry = 0;
        for(int i = 0; i < digits.length; i++){
            if(digits[digits.length - i - 1] + 1 > 9){
                carry = 1;
                digits[digits.length - i - 1] = 0;
            }else{
                digits[digits.length - i - 1] += 1;
                return digits;
            }
        }
        
        if(carry == 1){
            digits = new int[digits.length + 1];
            digits[0] = 1;
        }
        
        return digits;
    }

Log in to reply
 

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