Leetcode 66 Plus One
来源:程序员人生 发布时间:2016-11-21 09:06:09 阅读次数:2283次
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) {
int add=1;
for(int i=digits.size()⑴;i>=0;i--)
{
digits[i]=(digits[i]+add)%10;
if(digits[i]!=0) break;
}
if(digits[0]==0) digits.insert(digits.begin(),1);
return digits;
}
};
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠