Given a non-negative number represented asan array of digits, plus onetothenumber.
The digits are stored such that the most significant digit is atthe head ofthe list.
My Submitted Code
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int size=digits.size();
vector<int> q;
vector<int> result;
int carr=0;
int firstinloop=1;
for(int i=size-1;i>=0;i--){
int dig=0;
if(firstinloop){
dig=digits[i]+1+carr;
firstinloop=0;
}else{
dig=digits[i]+carr;
}
if(dig <= 9){
q.push_back(dig);
carr=0;
}else{
carr=1;
q.push_back(dig-10);
}
}
if(carr){
q.push_back(1);
}
while(!q.empty()){
result.push_back(q.back());
q.pop_back();
}
return result;
}
};
Plus OneGiven 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.My Submitted Codeclas