Accepted C Solution, too simple to explain


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

    a standard binary search with little modification.it's neat enough.


Log in to reply
 

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