1. 程式人生 > >LeetCode Plus One

LeetCode Plus One

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) {
        vector<int> ans(digits);
        reverse(ans.begin(),ans.end());

        int one = 1;
        for (int i = 0; i < ans.size(); i++) {
            ans[i] += one;
            one = ans[i] / 10;
            ans[i] %= 10;
        }

        if (one != 0) 
            ans.push_back(one);
        reverse(ans.begin(), ans.end());
        return ans;
    }
};