Given a number represented as an array of digits, plus one to the number.
Code:
class Solution { public: vector<int> plusOne(vector<int> &digits) { int n=digits.size(); bool carry=true; for(int i=n-1;i>-1;i--){ if(digits[i]==9&&carry==1){ digits[i]=0; carry=true; }else{ digits[i]+=carry; carry=false; } } if(carry) digits.insert(digits.begin(),1); return digits; } };