C++11 version, 3ms


  • 0
    G
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            vector<int> leadingOne = { 1 };
            for (auto it = digits.rbegin(); it != digits.rend(); ++it) {
                if (*it != 9) {
                    ++*it;
                    return digits;
                } else
                    *it = 0;
            }
            leadingOne.insert(leadingOne.end(), digits.begin(), digits.end());
            return leadingOne;
        }
    };
    

Log in to reply
 

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