很简单的一个题目,纯粹编码题,不详说了。
以下是源码:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
vector<int> ans;
int index = digits.size()-1;
int over = 1;
while(index>=0){
int res = digits[index] + over;
over = res/10;
res = res%10;
ans.push_back(res);
index--;
}
if (over == 1){
ans.push_back(over);
}
std::reverse(ans.begin(),ans.end());
return ans;
}
};
网友评论