Given a number represented as an array of digits, plus one to the number.
水题,练手感。
class Solution { public: vector<int> plusOne(vector<int> &digits) { int *a = new int[digits.size()+1]; memset(a, 0, digits.size()+1); int ret = 1, cur = 0; for (int i = digits.size() -1 ; i >= 0; i--) { if (digits[i] + ret == 10) a[cur++] = 0, ret = 1; else {a[cur++] = digits[i]+ret; ret = 0;} } if (ret == 1) a[cur++] = 1; vector<int> res; cur --; while (cur >=0) res.push_back(a[cur--]); return res; } };