题目:
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.
分析:
突然想到,很多时候我们需要认清自己。
新建一个容器,然后从后向前加即可。增加一个进位位,进位位初始值为1。
代码:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
vector<int> res;
vector<int>:: iterator it=digits.end()-1;
int cf=1;
while(it!=digits.begin()-1){
int now=*it+cf;
int iRes=now%10;
cf=now/10;
res.insert(res.begin(),iRes);
it--;
}
if(cf){
res.insert(res.begin(),cf);
}
return res;
}
};