Created
July 6, 2020 08:19
-
-
Save JyotinderSingh/dc44d0ed88c35930ca0b9028bfd3193a to your computer and use it in GitHub Desktop.
Plus One (LeetCode) | Interview Question Explanation
This file contains hidden or 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) { | |
digits.back()++; | |
for(int i = digits.size() - 1; i > 0 && digits[i] == 10; --i) { | |
digits[i] = 0; | |
digits[i - 1]++; | |
} | |
if(digits[0] == 10) { | |
digits[0] = 0; | |
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