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.
模拟大数加法加一,
注意判断首位是否有进位!
代码语言:javascript复制class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int add=1;
for(int i=digits.size()-1;i>=0;i--)
{
digits[i]=(digits[i] add);
if(digits[i]!=0) break;
}
if(digits[0]==0) digits.insert(digits.begin(),1);
return digits;
}
};