[leetcode]Plus One

Posted on 2013-07-25 18:59  冰天雪域  阅读(157)  评论(0编辑  收藏  举报
class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(digits.empty()) return vector<int>();
        
        int i = digits.size()-1;
        
        while(i >= 0 && digits[i] == 9){
            digits[i] = 0;
            i--;
        }
        
        if(i >= 0) digits[i]++;
        else digits.insert(digits.begin(),1);
        
        return digits;
        
    }
};


Copyright © 2024 冰天雪域
Powered by .NET 9.0 on Kubernetes