码迷,mamicode.com
首页 > 其他好文 > 详细

Search in Rotated Sorted Array II

时间:2015-03-18 14:04:10      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

 

bool Find(int A[], int first, int last,int target )
{
    int mid = (first + last) / 2;
	int leftbound = mid;
	int rightbound = mid;
	while (A[leftbound] == A[mid] && leftbound >= first)
		--leftbound;
	while (A[rightbound] == A[mid] && rightbound <= last)
		++rightbound;
	if (A[mid] == target)
		return true;
	else if (leftbound<first)
	{
		if (rightbound>last)
			return  false;
		else
			return Find(A, rightbound,last,target);
	}
	else
	{
		if (rightbound > last)
			return Find(A, first, leftbound, target);
		else
			return Find(A, rightbound, last, target) || Find(A, first, leftbound, target);
	}
}

bool search(int A[], int n, int target) {
	
	return Find(A, 0, n - 1, target);
}

Search in Rotated Sorted Array II

标签:

原文地址:http://blog.csdn.net/li_chihang/article/details/44409261

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!