LeetCode(67)Plus One

来源:互联网 发布:cocos2d-x用js编写 编辑:程序博客网 时间:2024/06/06 05:42

题目如下:

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) {        int new_digit = 0;        int over_flow = 0;        vector<int> result;        digits[digits.size() - 1] += 1;        for (int i = digits.size() - 1; i >= 0; --i) {            new_digit  = (over_flow + digits[i]) % 10;            over_flow  = (over_flow + digits[i]) / 10;            digits[i] = new_digit ;        }        result.assign(digits.begin(), digits.end());        if (over_flow > 0)            result.insert(result.begin(), 1);        return result;    }};




0 0
原创粉丝点击