Concise solution C++: plus any digit 0 to 9


  • 0
    C
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int plus = 1;
            for (auto p = digits.rbegin(); p != digits.rend(); p++) {
                int temp = *p + plus;
                *p = temp%10;
                plus = temp/10;
                if (!plus) break;
            }
            if (plus) digits.insert(digits.begin(), plus);
            return digits;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.