您的位置:首页 > 其它

Leetcode-66 Plus One

2016-11-21 16:59 288 查看
#66. Plus One

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 c=1;
for(int i=digits.size()-1;i>=0;i--)
{
int sum=digits[i]+c;
c=sum/10;
digits[i]=sum%10;
}
if(c==1)
{
digits.insert(digits.begin(),1);
}
return digits;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: