19.2.2 [LeetCode 33] Search in Rotated Sorted Array

Suppose an array sorted in ascending order 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 return -1.

You may assume no duplicate exists in the array.

Your algorithm‘s runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

题意

有序数组截成两段打乱顺序(或者也可能保持原序),以logn的复杂度找数

题解

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

先找从哪开始是分界点,然后再找数

原文地址:https://www.cnblogs.com/yalphait/p/10349317.html

时间: 2024-10-07 21:58:16

19.2.2 [LeetCode 33] Search in Rotated Sorted Array的相关文章

LeetCode 33 Search in Rotated Sorted Array [binary search] &lt;c++&gt;

LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前半部分接到后半部分后面,得到一个新数组,在新数组中查找给定数的下标,如果没有,返回-1.时间复杂度限制\(O(log_2n)\) C++ 我的想法是先找到数组中最大值的位置.然后以此位置将数组一分为二,然后在左右两部分分别寻找target. 二分寻找最大值的时候,因为左半部分的数一定大于nums[l],所以n

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (

LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

Suppose an array sorted in ascending order 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 return -1. You

leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法

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 retu

leetcode 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 return -1. You may assume no duplic

LeetCode 33 Search in Rotated Sorted Array(在旋转排序数组中搜索)(*)

翻译 假定一个数组在一个我们预先不知道的轴点旋转. 例如,0 1 2 4 5 6 7可能会变为4 5 6 7 0 1 2. 给你一个目标值去搜索,如果找到了则返回它的索引,否则返回-1. 你可以假定没有重复的元素存在于数组中. 原文 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

LeetCode 33 Search in Rotated Sorted Array (C,C++,Java,Python)

Problem: 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 return -1. You may assume

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

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