问题描述

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

参考答案

class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size() == ) return -;
int l = ;
int r = nums.size()-;
while(l<r){
int mid = (l+r)>>;
if(nums[l]<=nums[mid]){
if(target<=nums[mid] && target >= nums[l]) r = mid;
else l = mid +;
}else if(nums[mid]<nums[r]){
if(target>nums[mid] && target <= nums[r]) l = mid +;
else r = mid;
}
}
if(target == nums[l]) return r;
return -;
}
};

答案描述

由于不知道序列是否有序,因此在进行二分搜索的时候,需要先确定在什么区间:

1. 低位< 中位:意味着从 低位 到 中位,一定是有序的。如果循环的连接点,没有过中位,那么中位海拔更低,低位海拔更高,这个时候,有序的数列在,

2. 中位<高位:之间,这一段里面就是有序的,因此可以在这个区间内进行搜索。

宗旨,时刻保持有序。

LC 33. Search in Rotated Sorted Array的更多相关文章

  1. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  2. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  3. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  4. LeetCode题解33.Search in Rotated Sorted Array

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  5. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  6. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  7. 刷题33. Search in Rotated Sorted Array

    一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...

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

  9. 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. ...

随机推荐

  1. P1850 换教室——期望DP

    题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有 2n2n2n 节课程安排在 nnn 个时间段上.在第 iii(1≤i≤n1 \leq i ...

  2. MySQL 5.7:聊聊sql_mode

    1.sql_mode=only_full_group_by 导致的语法错误问题 MySQLSyntaxErrorException Caused by: com.mysql.jdbc.exceptio ...

  3. 说自己熟悉 Spring Cloud 这些面试题你会吗

    问题一:什么是Spring Cloud? Spring cloud流应用程序启动器是基于Spring Boot的Spring集成应用程序,提供与外部系统的集成.Spring cloud Task,一个 ...

  4. gacutil.exe的位置

    如果我们需要用gacutil去注册dll ,就需要使用Visual Studio的Command Prompt,前提是需要安装Visual Studio,但是客户端上一般是没有安装VS的,所以你就需要 ...

  5. sklearn.GridSearchCV选择超参

    from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.model ...

  6. java.lang.ClassNotFoundException:org.apache.struts2.dispatcher.FilterDispatcher

    老版本的Struts2升级,启动报的错. org.apache.struts2.dispatcher.FilterDispatcher 是web.xml中对struts2  2.2版本的接入点的类. ...

  7. delphi 获得父目录–指定级父目录

    function get_dir_parent(dir:string;n:integer):string; //n为几级父目录varst:string;i:integer;begin st:=GetC ...

  8. E: dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. 爆错解决办法

    author  :headsen chen date : 2019-06-06  10:09:06 root@ubuntu:~# apt-get remove java-1.8.0-openjdk E ...

  9. C语言练习,可以解析协议,主机,路径,询问,片段等

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...

  10. List的remove()方法的三种正确打开方式

    转: java编程:List的remove()方法的三种正确打开方式! 2018年08月12日 16:26:13 Aries9986 阅读数 2728更多 分类专栏: leetcode刷题   版权声 ...