LeetCode-Plus One

2018-10-27  本文已影响0人  圣地亚哥_SVIP

很简单的一个题目,纯粹编码题,不详说了。
以下是源码:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        vector<int> ans;
        int index = digits.size()-1;
        int over = 1;
        while(index>=0){
            int res = digits[index] + over;
            over = res/10;
            res = res%10;
            ans.push_back(res);
            index--;
        }
        if (over == 1){
            ans.push_back(over);
        }
        std::reverse(ans.begin(),ans.end());
        return ans;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读