Using Reverse Iterator


  • 0
    M

    vector<int> plusOne(vector<int>& digits) {
    int carry = 1;
    for(vector<int>::reverse_iterator it = digits.rbegin(); it != digits.rend(); it++) {
    *it = *it + carry;
    if(*it < 10) return digits;
    carry = *it / 10;
    *it = *it % 10;;
    }
    if(carry) digits.insert(digits.begin(), 1);
    return digits;


Log in to reply
 

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