1: class Solution {
2: public:
3: vector<int> plusOne(vector<int>& digits) {
4: int n = digits.size();
5: for (int i = n-1; i >= 0; i--) {
6: if (digits[i] == 9) digits[i] = 0;
7: else { digits[i]++; return digits; }
8: }
9: digits[0] = 1;
10: digits.push_back(0);
11: return digits;
12: }
13: };
Monday, July 18, 2016
66. Plus One
I was thinking of add 1 from end and insert 1 in the front if any carry. But I found the top rated solution is very concise and clever. Pushing a number to the back is much faster than inserting in the front.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment