Search in Rotated Sorted Array II

Question:

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.

Solution:

 1 class Solution {
 2 public:
 3     bool search(vector<int>& nums, int target)
 4     {
 5        int left=0;
 6     int right=nums.size()-1;
 7     if(left==right && nums[left]==target)
 8         return true;
 9     while(left!=right)
10     {
11         int mid=(left+right)/2;
12         if(nums[mid]==target) return true;
13         if(nums[left]<nums[mid])
14         {
15             if(nums[left]<=target && target<=nums[mid])
16                 right=mid;
17             else
18                 left=mid+1;
19         }
20         else if(nums[left]>nums[mid])
21         {
22             if(nums[mid]<=target && target<=nums[right])
23                 left=mid+1;
24             else
25                 right=mid;
26         }
27         else
28             left++;
29         if(left==right && nums[left]==target)
30             return true;
31     }
32     return false;
33     }
34 };

时间: 2024-10-18 20:39:49

Search in Rotated Sorted Array II的相关文章

【leetcode】Search in Rotated Sorted Array II

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. 与I类似,只是在二分搜

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 解题报告

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 (2 solutions)

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. 解法一:顺序查找 cl

[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

33. Search in Rotated Sorted Array &amp;&amp; 81. Search in Rotated Sorted Array II &amp;&amp;

33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise

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_81——Search in Rotated Sorted Array II(二分查找)

Search in Rotated Sorted Array II Total Accepted: 38274 Total Submissions: 121824My Submissions Question Solution Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and w

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