Help me with my solution, it won't pass the case [9,9]


  • 0
    W

    Please give some suggestions about the following solution. When the case is [9,9], the output is
    [1,824979803,824979803], why?

    vector<int> plusOne(vector<int> &digits) {
            //plus one at the digits' back
            int carry = 1;
            for (vector<int>::reverse_iterator it = digits.rbegin();it != digits.rend();++it){
                *it += carry;
                carry = 0;
                if (*it >= 10){
                    *it %= 10;
                    carry = 1;
                }
            }
            if (carry == 1){
                digits.push_back(0);
                for (vector<int>::reverse_iterator it = digits.rbegin();it != digits.rend() - 1;++it){
                    *it = *(it - 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.