标签:style blog io color ar os sp div on
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 jingwei=0; int len=digits.size();
int i=1; if((digits[len-1]+1)!=10)//不需要进位 {digits[len-1]=digits[len-1]+1;return digits;}
while(len-i>=0) { if(i==1&&(digits[len-i]+1)==10){ jingwei=1; digits[len-i]=0; } else if((digits[len-i]+jingwei)==10){ jingwei=1; digits[len-i]=0; }else if((digits[len-i]+jingwei)!=10){ jingwei=0; digits[len-i]=digits[len-i]+1; break; } ++i; } if(jingwei!=0) digits.insert(digits.begin(),1); return digits; } };
标签:style blog io color ar os sp div on
原文地址:http://www.cnblogs.com/fightformylife/p/4079304.html