simple C solution,using the same memory


  • 0
    W
    int* plusOne(int* digits, int digitsSize, int* returnSize) {
        int carry=1;
        * returnSize=digitsSize;
        for(int i=digitsSize-1;i>=0;i--){
        	digits[i]=digits[i]+carry;
        	if(digits[i]>=10){
        		digits[i]-=10;
        		carry=1;
        	}
        	else{
        		return digits;
        	}
        }
        if(carry){
        	digits=(int*)realloc(digits,(digitsSize+1)*sizeof(int));
        	memmove(digits+1,digits,digitsSize*sizeof(int));
        	digits[0]=carry;
        	* returnSize+=1;
        }
        return digits;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.