Leetcode | Plus One

Source: Internet
Author: User
Title: Given a number represented as an array of digits, plus one to the number.

idea: The traversal of an array needs to be done from right to left, and then the result exists in the temporary array as left-to-right. An array that traverses half the output before the array is reversed.
Code:
Class Solution {public
:
    vector<int> plusone (vector<int> &digits) {
        //Start typing your C/ C + + solution below
        //do not write int main () function
        vector<int> v;
        
        int next = 1;
        for (int i = Digits.size ()-1; I >= 0; i--)
        {
            int tmp = digits[i] + next;
            V.push_back (tmp%10);
            Next = TMP/10;
        }
        
        if (next! = 0)
        {
            v.push_back (next);
        }
        
        for (int i = 0; i < v.size ()/2; i++)
        {
            int t = v[i];
            V[i] = v[v.size ()-1-i];
            V[v.size ()-1-i] = t;
        }
        return v;
    }
};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.