首页 > 代码库 > LeetCode "Plus One"

LeetCode "Plus One"

The trick is, we can work on a reversed vector - learnt from EPI.

class Solution {public:    vector<int> plusOne(vector<int> &digits) {        std::reverse(digits.begin(), digits.end());        int i = 0;        digits[i] += 1;        while (digits[i] >= 10)        {            digits[i++] = 0;            if (i == digits.size())            {                digits.push_back(0);            }            digits[i] += 1;        }        std::reverse(digits.begin(), digits.end());        return digits;    }};