本文主要是介绍LeetCode OJ:Search in Rotated Sorted Array II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Search in Rotated Sorted Array II
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.
算法思想:
和Search in Rotated Sorted 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)/2;if(A[left]<target&&target<A[mid])right=mid-1;else 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 OJ:Search in Rotated Sorted Array II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!