Plus one

66. Plus One

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

S:特殊情况是digits[i]=9,更特殊的情况是数字为99...9,想好如何处理这两种情况即可

vector<int> plusOne(vector<int>& digits) {
        vector<int> result = digits;
        for(int i = result.size()-1; i >= 0; --i){
            if(result[i] == 9) result[i] = 0;
            else{ result[i] = result[i]+1;  return result;}
        }
        //会增加一位唯一的情况就是原数字是99..9
        result[0] = 1;
        result.push_back(0);
        return result;
    }

results matching ""

    No results matching ""