LeetCode——Search in Rotated Sorted Array II

892 ワード

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;
	}