66. Plus One

来源:互联网 发布:好用的隔离霜 知乎 编辑:程序博客网 时间:2024/05/21 01:30

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。

思路:加1,满10则进1.

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



0 0
原创粉丝点击