Total Accepted: 73167
Total Submissions: 233149
Difficulty: Easy
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.
Subscribe to see which companies asked this question
Hide Similar Problems
code:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len = digits.size();
reverse(digits.begin(), digits.end());
int carry=1;
for(int i=0;i<len && carry;i++) {
int tmp =digits[i] + carry;
carry = tmp / 10;
tmp %= 10;
digits[i]=tmp;
}
if(carry) digits.push_back(carry);
reverse(digits.begin(),digits.end());
return digits;
}
};