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

LeetCode——Search in Rotated Sorted Array II

时间:2014-08-09 11:38:57      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:leetcode

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.

原题链接:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/

题目:继续之前的题,在旋转过的有序数组中找目标值。若允许有重复的值呢?

	public boolean search(int[] A, int target) {
		int low = 0, high = A.length - 1;
		while (low <= high) {
			int mid = (low + high) / 2;
			if (target == A[mid])
				return true;
			if (A[mid] > A[low]) {
				if (target >= A[low] && target <= A[mid])
					high = mid - 1;
				else
					low = mid + 1;
			} else if (A[mid] < A[low]) {
				if (target >= A[mid] && target <= A[high])
					low = mid + 1;
				else
					high = mid - 1;
			} else
				low += 1;
		}
		return false;
	}



LeetCode——Search in Rotated Sorted Array II,布布扣,bubuko.com

LeetCode——Search in Rotated Sorted Array II

标签:leetcode

原文地址:http://blog.csdn.net/laozhaokun/article/details/38454343

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