Plus One

来源:互联网 发布:图标软件下载 编辑:程序博客网 时间:2024/04/28 01:14

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.


需要记录当前的仅为carry,而且别忘了要+1

class Solution {public:    vector<int> plusOne(vector<int> &digits) {        int carry = 0;        int pos = digits.size() - 1;        digits[pos] += 1;        for( ; pos >= 0; --pos){            int tmp = carry;//需要记录下            carry = ( carry + digits[pos]) / 10;            digits[pos] = (digits[pos] + tmp) % 10;        }        if( carry != 0)            digits.insert( digits.begin(), carry);        return digits;    }};


0 0
原创粉丝点击