Title: Given A number represented as an array of digits, plus one to the number.
Test instructions Dynamic array saves a number of single digits, forming a large number, and calculating the value after the large number plus one.
Simulates the addition of large numbers, which can be ejected when there is no carry.
Special consideration of each single digit is 9 of the situation, plus one more, that is, the result is 1 plus len a 0,len is the length of digits.
Class Solution {public
:
vector<int> plusone (vector<int> &digits) {
int i,p,temp,len= Digits.size ();
for (I=0;i<len;++i)
{
if (digits[i]!=9) break;
}
if (I==len)
{
vector<int>result;
Result.push_back (1);
for (i=0;i<len;++i) result.push_back (0);
return result;
}
Digits[len-1]+=1;
P=0;
for (i=len-1;i>=0;--i)
{
temp=digits[i]+p;
digits[i]=temp%10;
P=TEMP/10;
if (p==0) break;
}
return digits;
}
;
http://blog.csdn.net/havenoidea/