A 4ms Straightforward C++ Solution


  • 0
    Y
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int len = digits.size();
            int mark = 1;
            
            for(int i = len - 1; i >= 0; i--) {
                if((digits[i] + mark) <= 9) {
                    digits[i] = digits[i] + mark;
                    mark = 0;
                } else {
                    digits[i] = 0;
                    mark = 1;
                }
            }
            if(mark == 1) {
                digits.push_back(0);
                for(int i = 0; i < digits.size() - 1; i++) digits[i + 1] = digits[i];
                digits[0] = 1;
            }
            return digits;
        }
    };

Log in to reply
 

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