这个题很简单,实现一个加法器,唯一有用的地方是第一位进位的话要插入一个1
const int x = []{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return 0;
}();
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
vector<int>ans(digits);
int size = digits.size();
int carry = 1;
for(int i = size - 1; i >=0; i--){
ans[i] += carry;
if(ans[i] == 10 || ans[i] == 11){
carry = 1;
ans[i] %= 10;
}else{
carry = 0;
}
}
if(carry == 1) ans.insert(ans.begin(),1);
return ans;
}
};