Happyleetcode44:plus One

Source: Internet
Author: User

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

The digits is stored such, the most significant digit was at the head of the list

The title of the problem took a long time to understand. After understanding the good, feeling can also do.

It is important to note that when the number of results is increased, space needs to be increased for the vector. This requires the use of the insert in the vector. This is the first time to use, it feels good.

The code:

classSolution { Public: Vector<int> PlusOne (vector<int> &digits) {        intValue=digits.size ()-1;//Set Length         while(value>=0)        {            if(digits[value]<9) {Digits[value]+=1;  Break; }            Else{Digits[value]=0; Value--; }} Value++; if(value==0&&digits[value]==0) {Digits.insert (Digits.begin (),1);//This is a very important sentence .        }        returndigits; }};
Tell me about the use of insert in a vector object: vector container using the Insert () method, you can insert a new element in the vector object before any position, the book says, the Insert () method requires the location of the insertion, is the position of the element iterator, Rather than the subscript of the element. A new element is added to the Digits.begin () in the code above.

Happyleetcode44:plus One

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.