Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
题目理解了半天:才知道是它给的一个整数 该数组:该整数上每个位上的数值,然后对这个整数加一。输出变化后的结果
思想:主要注意的是如每个位是9的这个情况,加1后就要进位,最高位变0进一 相当于最高位变1,再在末尾插入1个地位0
有其中1位<9的话,加一,直接返回该数组
C++ AC代码:时间o(n)
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int index = digits.size()-1;
while(index>0){
if(digits[index]==9){
digits[index--] = 0;
}else{
digits[index--]++;
return digits;
}
}
if(digits[index]==9){
digits[index] = 1;
digits.push_back(0);
}else{
digits[index]++;
}
return digits;
}
};