We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 856773b commit 8db281eCopy full SHA for 8db281e
1 file changed
C++/plus-one.cpp
@@ -4,16 +4,17 @@
4
class Solution {
5
public:
6
vector<int> plusOne(vector<int>& digits) {
7
- vector<int> result(digits.cbegin(), digits.cend());
+ vector<int> result(digits.rbegin(), digits.rend());
8
int carry = 1;
9
- for (auto it = result.rbegin(); it != result.rend(); ++it) {
10
- *it += carry;
11
- carry = *it / 10;
12
- *it %= 10;
+ for (auto& num : result) {
+ num += carry;
+ carry = num / 10;
+ num %= 10;
13
}
14
if (carry == 1) {
15
- result.emplace(result.begin(), carry);
+ result.emplace_back(carry);
16
17
+ reverse(result.begin(), result.end());
18
return result;
19
20
};
0 commit comments