GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Algorithm/leetcode_solved/leetcode_0066_Plus_One.cpp at master · OneCodeMonkey/Algorithm Expand file tree/ Copy pathleetcode_0066_Plus_One.cppMore file actions 32 lines (30 loc) · 855 Bytes / Copy pathleetcode_0066_Plus_One.cppFile metadata and controls32 lines (30 loc) · 855 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 /** * AC: * Runtime: 4 ms, faster than 65.48% of C++ online submissions for Plus One. * Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Plus One. * 思路:写一个按位递进的判断,用一个变量记录上一个位置有无进位就行了。 * */ class Solution { public: vector<int> plusOne(vector<int>& digits) { int size = digits.size(); vector<int> ans; int flag = 1; // 是否需要进位 for(int i = size - 1; i >= 0; i--) { if(flag == 1) { if(digits[i] + 1 >= 10) { digits[i] = (digits[i] + 1) % 10; flag = 1; if(i == 0) { digits.insert(digits.begin(), 1); } } else { digits[i] += 1; flag = 0; } } } return digits; } }; |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |