leetcode 33: Plus One


Given a number represented as an array of digits, plus one to the number.



class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<int> rel( digits.size() );
        
        int carry = 1;
        int x = 0;
        //char c;
        
        for( int i=digits.size()-1; i>=0; i--) {
            x = digits[i]  + carry;
            rel[i] = x%10;
            carry = x/10;
        }
        
        if(carry) {
            rel.insert( rel.begin(), carry);
        }
        
        return rel;
    }
};


posted @ 2013-01-15 04:27  西施豆腐渣  阅读(115)  评论(0编辑  收藏  举报