LeetCode/solutions/66. Plus One.md
2019-09-13 23:08:41 +08:00

24 lines
770 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [66. Plus One](https://leetcode.com/problems/plus-one/description/)
# 思路
首先明确英语中digit代表0-9的一个数字。这题意思就是用一个数组代表一个数然后计算这个数加1后的结果。
从后往前遍历用一个c记录每一个digit所加的数初试为1以后每个c为前一次加法的进位。
注意如果最后有进位的话会使数组size加1
# C++
``` C++
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i = digits.size()-1;
int c = 1;
while(i >= 0){
digits[i] += c;
c = digits[i] / 10;
digits[i] %= 10;
i--;
}
if(c > 0) digits.insert(digits.begin(), c);
return digits;
}
};
```