> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-23-2022-66.md).

# 01/23/2022 66

![](/files/yKl0aJ3QoMaXrxhRbAxm)

Set sum = 0, carry = 1. Iterate from the back to the front,  sum = digits\[i] + carry, digits\[i] = sum % 10; carry = sum / 10. After the loop, if the carry > 0, then renew an array with n+ 1 length, set the first value = 1.

Time O(n)

Space O(N) worst case

```
        
```

class Solution {&#x20;

public int\[] plusOne(int\[] digits) {

```
    int n = digits.length;
    int carry = 1;
    int sum = 0; 
    for (int i = n -1; i >= 0; i--){
        sum = digits[i] + carry;
        digits[i] = sum % 10;
        carry = sum / 10;
    }   
    
    if(carry > 0){
        digits = new int[n + 1];
        digits[0] = 1;
        return digits; 
    }
    
    return digits;      
    
}
```

}
