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

leetcode - Search in Rotated Sorted Array II

时间:2014-10-12 16:06:48      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   ar   for   sp   on   cti   amp   

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.

class Solution {
public:
    bool search(int A[], int n, int target) {
		int left = 0,right = n - 1;
		while(left <= right)
		{
			int mid = left + (right - left) / 2;
			if(A[left] < target && target < A[mid]) right = mid - 1;
			if(A[mid] < target && target < A[right])left  = mid + 1;
			else
			{
				if(A[left] != target) left++;
				else return true;
				if(A[right]!= target) right--;
				else return true;
			}
		}
		return false;
    }
};


leetcode - Search in Rotated Sorted Array II

标签:style   color   io   ar   for   sp   on   cti   amp   

原文地址:http://blog.csdn.net/akibatakuya/article/details/40017577

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