problem:
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.
Array Binary Searchthinking:
(1)出题人想考察二分法,但是该题的最简单的算法是遍历查找法,时间复杂度为O(n)
(2)回到二分法,再确定mid的左、右侧位置的位置时,考虑到有重复元素的出现,所以每次都要沿着mid往两遍遍历,时间复杂度最坏也可以达到O(n)
code:
遍历法:
class Solution {
public:
bool search(int A[], int n, int target) {
if(NULL == A || 0 == n)
return false;
for(int i = 0; i < n; ++i)
if(A[i] == target)
return true;
return false;
}
};leetcode || 81、Search in Rotated Sorted Array II
原文地址:http://blog.csdn.net/hustyangju/article/details/44981635