Plus One
题目描述:
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.
英语不好,第一句愣是没看懂。其实就是用一个数组表示一个整数,比如数字123,数组对应存放1、2、3三个数字。
solution:
vector<int> plusOne(vector<int>& digits) { if(digits.empty()) return digits; int i = digits.size() - 1; while(i >= 0 && digits[i] == 9) digits[i--] = 0; if(i >= 0) digits[i]++; else { digits[0] = 1; digits.push_back(0); } return digits; }