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