题目链接https://leetcode.com/problems/plus-one/
这是digit这类题里最简单的一道了,这类题基本都不难,但是需要把几个boundary case考虑到,这道题里需要考虑的是进位之后首位的情况。其他case以后遇到再提。
class Solution {public: vector plusOne(vector & digits) { int carry = 1; vector result; for(int i = digits.size() - 1; i >= 0; i--) { int cur = digits[i] + carry; result.push_back(cur % 10); carry = cur / 10; } if(carry > 0) result.push_back(carry); reverse(result.begin(), result.end()); return result; }};