66.Plus One

思路:
  • 直接模拟加法
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int lead = 0;
        int in = 1;
        for(int i = digits.size()-1; i >= 0; i--){
            if(digits[i] + in >= 10){
                digits[i] = (digits[i] + in)%10;
                in = 1;
            }else{
                digits[i] = (digits[i] + in)%10;
                in = 0;
            }
        }
        vector<int> res;
        if(in == 1){
            res.push_back(in);
            for(int i = 0; i < digits.size(); i++){
                res.push_back(digits[i]);
            }
            return res;
        }else return digits;
    }
};
posted @ 2017-06-11 08:22  UniMilky  阅读(159)  评论(0编辑  收藏  举报