66. Plus One

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int carry = 0, n= digits.size()-1;
while(n>-1){
if(digits[n] == 9){
digits[n]= 0;
n--;
}
else{
digits[n]++;
break;
}
}
if(digits[0] == 0) digits.insert(digits.begin() ,1);

return digits;

}
};

analysis

  • time complexity O(n)
  • space complexity O(1)