Created
November 5, 2018 10:29
-
-
Save fpdjsns/9cd1133c760c32d2c2b2bef6ae0a3656 to your computer and use it in GitHub Desktop.
[leetcode] 66. Plus One : https://leetcode.com/problems/plus-one/
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 carry = true; | |
vector<int> ans(digits.size(), 0); | |
for(int i=digits.size()-1;i>=0;i--){ | |
int sum = carry + digits[i]; | |
carry = sum / 10; | |
ans[i] = sum % 10; | |
} | |
if(carry) ans.insert(ans.begin(), 1); | |
return ans; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment