[LeetCode]题解(python):081 - Search in Rotated Sorted Array II

题目来源



https://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 a given target is in the array.



题意分析



Input:

:type nums: List[int]
:type target: int

Output:

rtype: bool

Conditions:一个翻转的有序数组,数组元素可能重复,判断target是否在数组中



题目思路



关键就要区分边界,采用first表示下界,last表示上界,mid为中间点。如果mid为target,返回True;否则,判断mid,first,last是否相等,若相等则缩小搜索空间,之后判断target在哪个区间,判断条件为:1)target与nums[mid]的大小,target与nums[first]的大小。



AC代码(Python)

 1 class Solution(object):
 2     def search(self, nums, target):
 3         """
 4         :type nums: List[int]
 5         :type target: int
 6         :rtype: bool
 7         """
 8         size = len(nums)
 9         first = 0
10         last = size - 1
11         while first <= last:
12             mid = (last + first) / 2
13             print(first,mid, last)
14             if nums[mid] == target:
15                 return True
16             if nums[mid] == nums[first] == nums[last]:
17                 first += 1; last -= 1
18             elif nums[first] <= nums[mid]:
19                 if target < nums[mid] and target >= nums[first]:
20                     last = mid - 1
21                 else:
22                     first = mid + 1
23             else:
24                 if target >= nums[mid] and target < nums[first]:
25                     first = mid + 1
26                 else:
27                     last = mid - 1
28
29
30
31         return False
32         
时间: 2024-10-31 01:36:42

[LeetCode]题解(python):081 - Search in Rotated Sorted Array II的相关文章

Java for LeetCode 081 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. 解题思路: 参考Java for LeetCode 033 Search in Rota

081. Search in Rotated Sorted Array II

1 class Solution { 2 public: 3 bool search(vector<int>& nums, int target) { 4 if (nums.size() == 0) return false; 5 else { 6 int first = 0, last = nums.size() - 1; 7 while (first <= last) { 8 int mid = first + (last - first) / 2; 9 if (nums[m

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)方法二 : 实现:  

[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 [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

Search in Rotated Sorted Array II leetcode java

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

【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. 题解:如果没有重复的元素,那么就可以根据target是否在某一半而扔掉另外一半.但是如果有

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 [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