leetcode刷题,总结,记录,备忘66

来源:互联网 发布:python冷门知识 编辑:程序博客网 时间:2024/06/06 08:39

leetcode66Plus 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.

从尾巴开始找,加1,满10就进1,还是比较容易的。

class Solution {public:    vector<int> plusOne(vector<int>& digits) {                for (int i = digits.size()-1; i >= 0; --i)        {            if (i == digits.size()-1 && digits[i] == 9)            {                digits[i] = 0;                continue;            }            else if (i == digits.size()-1 && digits[i] != 9)            {                digits[i] += 1;                break;            }                        if (digits[i] == 9)            {                digits[i] = 0;                continue;            }            else            {                digits[i] += 1;                break;            }        }                vector<int> temp;        temp.push_back(1);        if (digits[0] == 0)        {            temp.insert(temp.end(), digits.begin(), digits.end());            return temp;        }        else        {            return digits;        }    }};


0 0
原创粉丝点击