Java solution


  • 0
    L
    public int[] plusOne(int[] digits) {
            if(digits == null || digits.length == 0)
           return new int[]{};
           int carry = 1; 
           int[] res = new int[digits.length];
    
           for(int i=digits.length-1; i>=0; i--){
    
               int sum = digits[i]+carry;
               res[i] = sum%10;
               carry = sum/10;
           }
    
           if(carry == 1){
               int[] res2 = new int[digits.length+1];
               res2[0] = 1;
               for(int i=0; i<res.length; i++){
                   res2[i+1] = res[i];
               }
    
               return res2;
           }
    
           return res;
        }
    

Log in to reply
 

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