您的位置:首页 > 其它

Plus One

2015-07-08 22:32 561 查看
Question:

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.

Solution:

class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
reverse(digits.begin(),digits.end());
int t=digits[0]+1;
int val=t%10;
int up=t/10;
*(digits.begin())=val;
for(auto iter=digits.begin()+1;iter!=digits.end();iter++)
{
t=*iter+up;
val=t%10;
up=t/10;
*iter=val;
}
if(up)
digits.push_back(up);
reverse(digits.begin(),digits.end());
return digits;
}
};


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: