forked from livingstream/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne.cpp
More file actions
29 lines (26 loc) · 808 Bytes
/
PlusOne.cpp
File metadata and controls
29 lines (26 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//============================================================================
// PlusOne
// Given a number represented as an array of digits, plus one to the number.
//============================================================================
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
reverse(digits.begin(), digits.end());
int carry = 1;
for (size_t i = 0; i < digits.size(); i++) {
int sum = digits[i] + carry;
digits[i] = sum % 10;
carry = sum / 10;
if (carry == 0) break;
}
if (carry != 0) digits.push_back(carry);
reverse(digits.begin(), digits.end());
return digits;
}
};
int main() {
return 0;
}