Created
April 15, 2014 06:53
-
-
Save 201power/10708508 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> plusOne(vector<int> &digits) { | |
int i,inc=0; | |
digits.back()=digits.back()+1; | |
if (digits.back() > 9){ | |
inc=1; | |
digits.back()=digits.back()-10; | |
i=digits.size()-2; | |
while (i>=0) { | |
digits[i]=digits[i]+inc; | |
if (digits[i]>9){ | |
digits[i]=digits[i]-10; | |
i--; | |
} | |
else{ | |
inc=0; | |
break; | |
} | |
} | |
} | |
if (inc==1) | |
digits.insert(digits.begin(),1); | |
return digits; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment