Accepted C Solution, too simple to explain


  • 0
    R
    int searchInsert(int* nums, int numsSize, int target) { 
    	if(0==numsSize) return 0;
    
    	int p1=0;
    	int p2=numsSize-1;
    	int cur;
    
    	while(p1<=p2){
    		cur=(p1+p2)/2;
    		if(target==nums[cur]){
    			break;
    		}
    		if(target<nums[cur]){
    			p2=cur-1;
    		}else{
    			p1=cur+1;
    		}
    	}
    	if(target>nums[cur]){
    		++cur;
    	}
    	return cur;
    }
    

    well,this is a standard binary search thing.not too much to explain.


Log in to reply
 

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