Fairly easy 4ms c++


  • 0
    T
    vector<int> plusOne(vector<int>& digits) {
        std::reverse(digits.begin(), digits.end()); 
        auto carry = 1; 
        for(auto &x : digits) {
            if(carry == 0) 
                break; 
                
            ++x; 
            if(x > 9)
                x = 0; 
            else  
                carry = 0; 
        }
        if(carry) 
            digits.push_back(1); 
        std::reverse(digits.begin(), digits.end()); 
        return digits; 
    }

Log in to reply
 

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