您的位置:首页 > 其它

leetcode Plus ONE

2015-10-14 21:29 387 查看
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.

分析:这是一个进位加法运算,设置进位变量add,初始值1,判断最低位加1后是否满10,满10则add=1,否则=0;如此循环。直至结束:

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