求双调节点

如果一个数组是双调的,即1,2,3,6,4,2或者7,6,4,3,5,8,9我们需要能够找到双调的中心节点在什么位置。

int DoubleTuning(int* list, int length)
{

	int* mid = list + length / 2;
	int* start = list;
	int* end = list+length - 1;
	
	while(mid < end)
	{
		int* low = mid;
		int* high = mid;
		while(*low == *(low - 1) && low > start)
		{
			low = low -1;
		}
		low = low - 1;
		while(*high == *(high + 1) && high < end)
		{
			high = high +1;
		}
		high = high + 1;
		if((*mid > *low && *mid > *high) ||(*mid < *low && *mid < *high))
		{
			return mid - list;
		}
		else if( (*mid > *start && *mid > *low )||(*mid < *start && *mid < *low))
		{
			start = mid ;
			mid = start + (end - start) /2;
			
		}
		else
		{
			end = mid ;
			mid = start + (end - start) /2;
		}
	}
	return -1;
}

 

点赞