Plus One

来源:互联网 发布:淘宝企业店铺怎么登录 编辑:程序博客网 时间:2024/06/07 19:27

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.

给定一组数字代表一个非负数,让这个数加一。

从最后开始加,满10就减10并令c=1,代码如下:

class Solution {public:    vector<int> plusOne(vector<int> &digits) {        int c = 1;        for(int i =digits.size()-1;i >= 0;i--)        {            digits[i] += c;            c = digits[i] / 10;            digits[i] %= 10;        }        if(c > 0)        {            digits.insert(digits.begin(),c);        }        return digits;    }};


0 0
原创粉丝点击