Created
January 10, 2021 06:55
-
-
Save ceth-x86/3d58e7ca343ef4a6795d732738080e7a 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) { | |
bool next_step = true; | |
int pos = digits.size() - 1; | |
while (next_step && (pos >= 0)) { | |
digits[pos] += 1; | |
if (digits[pos] == 10) { | |
digits[pos] = 0; | |
next_step = true; | |
} else { | |
next_step = false; | |
} | |
pos -= 1; | |
} | |
if (next_step) { | |
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