forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlus_One.cpp
41 lines (37 loc) · 818 Bytes
/
Plus_One.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
/*
Author: Weixian Zhou, [email protected]
Date: Jul 11, 2012
Problem: Plus One
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
Given a number represented as an array of digits, plus one to the number.
Solution:
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
vector<int> result;
int carry = 1;
result.clear();
for (int i = digits.size() - 1; i >= 0; i--) {
int sum = digits[i] + carry;
digits[i] = sum % 10;
carry = sum / 10;
}
if (carry) {
result.push_back(1);
}
result.insert(result.end(), digits.begin(), digits.end());
return result;
}
};