【题目描述】
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.
【思路】纯模拟题。
【代码】
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len=digits.size();
if(digits[len-1]!=9){
digits[len-1]=digits[len-1]+1;
}
else{
for(int i=len-1;i>=0;i--){
if(digits[i]==9){
digits[i]=0;
}
else{
digits[i]+=1;
break;
}
}
}
if(digits[0]==0){
digits.insert(digits.begin(),1);
}
return digits;
}
};
410

被折叠的 条评论
为什么被折叠?



