class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int i,j,carry=1;
// traditional long addition..
for(i=digits.size()1;i>=0&&carry;i){
int sum=carry+digits[i];
carry=sum/10;
digits[i]=sum%10;
}
if(carry){ // if carry is 1, then digits must be all 99..999
digits[0]=1;
digits.push_back(0);
}
return digits;
}
};
Fastest and simplest C++ solution


branching may be more costly. you may try using bit hacks and twiddling to avoid % and /. For example, you can exploit this fact :
digits[i] == sum iff carry == 0, digits[i] == 0 if carry == 1.
to get
digits[i] = sum & ~(carry)
I personally wouldn't recommend this kind of microoptimisation unless it is absolutely necessary.