-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlus_One.cpp
More file actions
47 lines (39 loc) · 1.04 KB
/
Plus_One.cpp
File metadata and controls
47 lines (39 loc) · 1.04 KB
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
42
43
44
45
46
// Source : https://oj.leetcode.com/problems/plus-one/
// Author : zheng yi xiong
// Date : 2015-02-02
/**********************************************************************************
*
* Given a non-negative number represented as an array of digits, plus one to the number.
* The digits are stored such that the most significant digit is at the head of the list.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int carry = 1;
for (vector<int>::reverse_iterator reIter = digits.rbegin(); reIter != digits.rend(); ++reIter)
{
*reIter += carry;
carry = (*reIter) / 10;
*reIter = (*reIter) % 10;
}
if (0 < carry)
{
digits.insert(digits.begin(), carry);
}
return digits;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> digits;
digits.push_back(9);
Solution so;
so.plusOne(digits);
return 0;
}