Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
題意:給定一個由數組表示大整數,數組的每一個元素對應該數的十進制表示的每一位,對該數進行加 1 操作
思路:高精度加法
復雜度:時間:O(n)
vectorplusOne(vector &digits){ int carry = 1; for(auto it = digits.rbegin(); it != digits.rend(); ++it){ int tmp = *it + carry; *it = tmp % 10; carry = tmp / 10; if(!carry) break; } if(carry) digits.insert(digits.begin(), carry); return digits; }