Leetcode | | 66, Plus One

Source: Internet
Author: User

Problem:

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.

Hide TagsArray MathTest instructions, a non-negative integer is saved in the form of an array, each bit 0~9, the number +1, returns its array form, requiring the highest number of digits in the array first

Thinking:

(1) First to read test instructions, this problem is not difficult.

(2) Attention to detail:

1.

while (a>0 &&--i>=0)       {            int tmp=digits[i];           digits[i]= (Digits[i]+a)%10;           A= (Tmp+a)/10;       }
Application of temporary variables

2, the last one should not carry, to consider the

Code

Class Solution {public:    vector<int> plusone (vector<int> &digits) {        int a=0;        if (Digits.size () ==0)            {                digits.push_back (1);                return digits;            }        int I=digits.size ()-1;         A= (digits[i]+1)/10;        digits[i]= (digits[i]+1)%10;       while (a>0 &&--i>=0)       {            int tmp=digits[i];           digits[i]= (Digits[i]+a)%10;           A= (Tmp+a)/10;       }       if (a>0)            Digits.insert (Digits.begin (), a);        return digits;}    ;


Leetcode | | 66, 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.