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运算,这里需要注意进位的情况。按以下思路很容易解决:
对数字的末位加1,如果和小于10,则没有进位,直接输出;否则将其置0,进1位,如此循环。如果最终首位为0,很明显数字长度将增加一位,首位数字为1,其它为全为0,AC代码如下:
vector<int> plusOne(vector<int>& digits)
{
for (int i=digits.size()-1; i>=0; i--)
{
digits[i]++;
if (digits[i]<10)
break;
digits[i]=0;
}
if (digits[0]==0)
{
digits[0]=1;
digits.push_back(0);
}
return digits;
}原文地址:http://blog.csdn.net/lsh_2013/article/details/45725555