标签:
在 033 Search in Rotated Sorted Array 稍作改进, 但是最坏情况下 复杂度为O(n)
class Solution: # @param A, a list of integers # @param target, an integer to be searched # @return an integer def search(self, A, target): if len(A) == 0: return False return self.solve(A, target, 0, len(A)- 1) def solve(self, a, target, start, end): if start > end: return False half = int((end-start)/2) + start if a[start] == target: return True if a[end] == target: return True if a[half] == target: return True if a[start] < a[half]: if target > a[half] or target < a[start]: return self.solve(a, target, half+1, end-1) else: return self.solve(a, target, start+1, half-1) elif a[start] > a[half]: if target < a[half] or target > a[start]: return self.solve(a, target, start+1, half-1) else: return self.solve(a, target, half+1, end-1) else: return self.solve(a, target, start+1, half-1) or self.solve(a, target, half+1, end-1)
081 Search in Rotated Sorted Array II
标签:
原文地址:http://www.cnblogs.com/dapanshe/p/4629870.html