A 4ms C++ solution


  • 0
    Y
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int len = digits.size();
            int i = len - 1;
            bool addflag = true;
    
            while((i >= 0)&&(addflag)) {
                digits[i] += 1;
                if(digits[i] == 10) {
                    digits[i] = 0;
                } else addflag = false;
                i--;
            }
    
            if((i < 0)&&addflag) {
                digits.push_back(0);
                for(int j = digits.size() - 1; j >= 1; j--) {
                    digits[j] = digits[j - 1];
                }
                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.