Skip to content

Instantly share code, notes, and snippets.

@JyotinderSingh
Created July 6, 2020 08:19
Show Gist options
  • Save JyotinderSingh/dc44d0ed88c35930ca0b9028bfd3193a to your computer and use it in GitHub Desktop.
Save JyotinderSingh/dc44d0ed88c35930ca0b9028bfd3193a to your computer and use it in GitHub Desktop.
Plus One (LeetCode) | Interview Question Explanation
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