
66
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
-
element)```: The vector is extended by inserting new elements before the element at the specified *position*, effectively increasing the container size by the number of elements inserted. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- ```vector::pop_back()```: Removes the last element in the vector, effectively reducing the container size by one.
- ```vector::back()```: Returns a reference to the last element in the vector.
```c++
vector<int> plusOne(vector<int> &digits) {
for (int i = digits.size() - 1; i >= 0; --i) {
digits[i] += 1;
if (digits[i] == 10) {
digits[i] = 0;
} else {
break;
}
}
if (digits[0] == 0) {
digits.insert(digits.begin(), 1);
}
return digits;
}




近期评论