Skip to content

Latest commit

 

History

History

66.Plus-One

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

66. Plus One

Untitled

  • consider bit overflow, arr length might be greater than 32/64
  • only all 9s needs to grow the arr length for 1
class Solution {
    public int[] plusOne(int[] digits) {
        int len = digits.length;
        boolean carry = true;
        
        for(int i = len - 1; i >= 0; i --) {
            int val = 0;
            if(carry) val = 1;
            else return digits;
           
            int sum = val + digits[i];
            carry = sum > 9;
            digits[i] = sum % 10;
        }
        
        if(carry){
            int[] res = new int[len + 1];
            // only all 9s needs to grow the array
            res[0] = 1;
            return res;
        }
        
        return digits;
    }
}