Given a non-negative integer represented asa non-empty array of digits, plus onetotheinteger.
You may assume theintegerdonot contain any leading zero, except thenumber0 itself.
The digits are stored such that the most significant digit is atthe head ofthe list.
测试代码(c++):
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int c = 0;
int i = digits.size()-1;
digits[i] += 1;
while(i>=0)
{
digits[i] += c;
c = digits[i]/10;
digits[i] = digits[i]%10;
i--;
}
if(c)
{
digits.insert(digits.begin(),1);
}
return digits;
}
};
性能:
参考答案(python):
classSolution(object):defplusOne(self, digits):"""
:type digits: List[int]
:rtype: List[int]
"""
num = 0for i in range(len(digits)):
num += digits[i] * pow(10, (len(digits)-1-i))
return [int(i) for i in str(num+1)]