给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。
该数字按照数位高低进行排列,最高位的数在列表的最前面。
样例
样例 1:
输入:[1,2,3]
输出:[1,2,4]
样例 2:
输入:[9,9,9]
输出:[1,0,0,0]
class Solution {
public:
/**
* @param digits: a number represented as an array of digits
* @return: the result
*/
vector<int> plusOne(vector<int> &digits) {
// write your code here
int jinwei = 1;
int size = digits.size();
int i = 0;
for(i=size-1; i>=0; i--)
{
int tmp = digits[i] + jinwei ;
if(tmp >= 10)
{
digits[i] = tmp % 10;
jinwei = 1;
}
else if(tmp < 10)
{
digits[i] = tmp;
jinwei = 0;
}
}
if(jinwei == 1)
{
digits.insert(digits.begin(),1);
}
return digits;
}
};