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[mid] == target) return true;
10                 else {
11                     if (nums[first] < nums[mid]) {
12                         if (nums[first] <= target && target < nums[mid]) last = mid - 1;
13                         else first = mid + 1;
14                     }
15                     else if (nums[first] > nums[mid]) {
16                         if (nums[mid] < target && target <= nums[last]) first = mid + 1;
17                         else last = mid - 1;
18                     }
19                     else {
20                         ++first;
21                     }
22                 }
23             }
24             return false;
25         }
26     }
27 };
时间: 2024-08-29 08:33:14

081. Search in Rotated Sorted Array II的相关文章

[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

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

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