[Leetcode]66. Plus One

来源:互联网 发布:网络盗刷信用卡能查到 编辑:程序博客网 时间:2024/06/05 01:12

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) {        add(digits, 1);        return digits;    }private:    void add(vector<int>& digits, int digit)    {        int c = digit;        for_each (digits.rbegin(), digits.rend(), [&c](int &d)        {            d += c;            c = d / 10;            d %= 10;        });        if (c > 0)            digits.insert(digits.begin(), 1);    }};

0 0