题目
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.
分析
该题目要求:将一整数按位存储在vector中,对其实现
是一道简单题目,对存储序列vector中元素,倒序遍历,末位
若此时,进位依然为
AC代码
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len = digits.size();
if (len == 0)
return digits;
int carry = 1;
for (int i = len - 1; i >= 0; --i)
{
digits[i] += carry;
if (digits[i] >= 10)
{
digits[i] -= 10;
carry = 1;
continue;
}
else{
carry = 0;
break;
}
}
if (carry == 0)
return digits;
else
{
vector<int> v;
v.push_back(1);
for (int i = 0; i < len; i++)
v.push_back(0);
return v;
}
}
};