leetcode 【 Search in Rotated Sorted Array II 】python 实现

题目

与上一道题几乎相同;不同之处在于array中允许有重复元素;但题目要求也简单了,只要返回true or false

http://www.cnblogs.com/xbf9xbf/p/4254590.html

代码:oj测试通过 Runtime: 73 ms

 1 class Solution:
 2     # @param A a list of integers
 3     # @param target an integer
 4     # @return a boolean
 5     def search(self, A, target):
 6         A=list(set(A))
 7         # none case & zero case
 8         if A is None or len(A)==0 :
 9              return False
10         # binary search
11         start = 0
12         end = len(A)-1
13         while start<=end :
14             # one element left case
15             if start == end :
16                 if A[start]==target :
17                     return True
18                 else:
19                     return False
20             # two elements left case
21             if start+1 == end :
22                 if A[start]==target :
23                     return True
24                 elif A[end]==target :
25                     return True
26                 else:
27                     return False
28             # equal or more than three elements case
29             mid = (start+end)/2
30             if A[mid]==target :
31                 return True
32             elif A[mid]>target:
33                 if A[start]>A[mid] and A[end]<A[mid]:
34                     start = mid+1
35                 elif A[start]<A[mid] and A[end]<A[mid]:
36                     if A[end]>=target:
37                         start = mid+1
38                     else:
39                         end = mid-1
40                 elif A[start]>A[mid] and A[end]>A[mid]:
41                      end = mid-1
42                 else:
43                      end = mid-1
44             else:
45                 if A[start]>A[mid] and A[end]<A[mid]:
46                     end = mid-1
47                 elif A[start]<A[mid] and A[end]<A[mid]:
48                     start = mid+1
49                 elif A[start]>A[mid] and A[end]>A[mid]:
50                     if A[end]>=target :
51                         start = mid+1
52                     else:
53                         end = mid-1
54                 else:
55                     start = mid+1
56         return False

思路

用了一个trick Python数组去重的办法A=list(set(A))

这样A数组中就没有重复的元素了,可以直接用之前一题的代码。

这样的trick应该不是题目的本意,这道二分查找题目比较经典,应该吃透。

时间: 2024-10-11 18:18:35

leetcode 【 Search in Rotated Sorted Array II 】python 实现的相关文章

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/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

[LeetCode] Search in Rotated Sorted Array II [36]

题目 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

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/search

LeetCode: Search in Rotated Sorted Array II [081]

[题目] 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 Ar

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: 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 arr

leetcode:Search in Rotated Sorted Array II(duplicated)

题目要求: 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://leetcode.com/problems/sear

[leetcode]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&q

[LeetCode] 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. Hide Tags Array Binary Search 这个是在 前一道的基础上改过来

LeetCode OJ: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. 这个和前面的一个不一样就在于可能会有重复的数字,那么判断的时候就应该注意了,遇到start

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: 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. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现: