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”的基础上,现在允许数组中出现重复值。问是否仍然能够使用原来的方法来执行搜索。
因为重复值的存在,反转边界很可能无法区分,即出现A[0]==A[n-1]的情况,是的原来利用二叉搜索的方法就失效了。
class Solution { public: bool search(int A[], int n, int target) { for(int i=0; i<n; i++){ if(A[i]==target)return true; } return false; } };
LeetCode: Search in Rotated Sorted Array II [081],布布扣,bubuko.com
LeetCode: Search in Rotated Sorted Array II [081]
原文地址:http://blog.csdn.net/harryhuang1990/article/details/27638613