LeetCode066 Plus one C language

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.

Test instructions: A non-negative integer is stored in the form of an array. Among them high in a[0]. It then adds 1 to this number and returns the array.

PS: Forgive me again have read TEST instructions---!

/** * return an array of size *returnsize. * note: the  Returned array must be malloced, assume caller calls free ().  */ Int* plusone (int* digits, int digitssize, int* returnsize)  {     int i;    int index=0;    int carry=0;     int flag=1;    //feel is to steal a lazy, only when all is 9 in the time only into 1 ... So     //only single digit plus 1, not each plus, so with flag    for (i=digitssize-1;i>=0;i--) {         //not each add 1        if ( digits[i]+flag+index>9) {            digits[i]=0 ;            index=1;             if(i==0) {                carry=1;             }         }else{            digits[i]=digits[i]+ 1;            break;         }        flag=0;         // printf ("%d", Digits[i]);    }         //This value also must write understand, otherwise the program does not know???     *returnsize=digitssize+carry;    if (Carry) {         int *newdigits= (int*) malloc (sizeof (int) *digitssize+1);         newdigits[0]=1;        for (I=1;i<digitssize+1;i++) {            newdigits[i]=0;         }        return  newdigits;    }else{        return digits;     }       }

PS: It's written in a daze ..... Do not perform ..... Look at the online program seems to be the end of the return to the returnsize ... That's all we can do.

In fact, only 9 of the time will produce the first rounding ....

LeetCode066 Plus one C language

Related Article

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.