leetcode 066 —— Plus One

来源:互联网 发布:外贸宝海关数据 编辑:程序博客网 时间:2024/06/05 17:57

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.

思路:模拟整数的加法,这个比较简单

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



0 0
原创粉丝点击