Leetcode:plus One

Source: Internet
Author: User

First, the topic

The large number that is saved with an array, each element represents a number of one, and the number is added 1 to the size of the change.

Second, analysis

See this one we'll think about when we're dealing with large numbers. The use of high-precision calculation methods, we need to pay attention to the following areas:

1. Start traversing from the last face of the array

2, the current number plus low carry

3. Carry the current bit to the high position

4, each time to get a carry judgment is 0, 0 can be directly ended, to a certain extent, speed up the efficiency of the algorithm.

5. The last one needs to be aware that if the highest bit has carry, you need to add an element to the front.

Class Solution {public:    vector<int> plusone (vector<int> &digits) {    int len = Digits.size ();    int carry = 1;    int flag;        for (int i=len-1;i>=0;i--) {        digits[i]+=carry;        flag = digits[i]%10;        carry = DIGITS[I]/10;        Digits[i] = flag;        if (!carry) break            ;        }         if (carry)        Digits.insert (Digits.begin (), carry);        return digits;}    ;

Leetcode: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.