LeetCode OJ 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 duplicate exists in the array.
【题目分析】dan'sh
有一个没有重复元素的增序排序的数组,对这个数组进行玄旋转操作,然后在旋转后的数组中找到给定的target,如果找不到则返回-1。
【思路】
在一个有序的数组中查找一个数,最高效的就是二分查找。但是经过旋转操作以后数组分裂成了两段,在每一段中是有序的,这中情况下我们能对它使用二分查找嘛?
既然数组被分成了两段,如果我们使用二分查找能确定目标值在数组的哪段,然后改变查找的范围,那么就可以找到目标值。在二分查找的过程中要先确定中间位置的下标mid,通过中间值与最左边值的比较,我们可以确定中间值在数组中的哪一段,然后我们在比较目标值和中间值的大小,这样就能确定目标值的范围。举个例子如下:
- 目标值为6,nums[mid] = 7 > nums[left]。因此中间值在左边一段,此时nums[left]<target<=nums[mid],可以确定目标值在左边一段。right = mid;

- nums[mid] = 5 > nums[left]。因此中间值在左边一段,此时target>nums[mid]。left = mid + 1;

- nums[mid] = 6 = nums[left]。因此中间值在左边一段,此时target=nums[mid]。right = mid;

- left = right,找到目标值;

注意:
- 当中间值在数组的左段时,要确定目标值的位置不能这样判断:if(target <= nums[mid]),因为我们发现此时数组右段也是满足这个条件的,因此必须这样判断:if(target <= nums[mid] && target >= nums[left]),这样才能保证目标值位与中间值的左边,对于其他的情况目标值肯定在中间值的右边。
- 同理,当中间值在数组的右段时候,在确定目标值的位置时也要注意类似的情况。
- 这个算法对于一个没有旋转的增序排序数组也是适用的。
【java代码】
public class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) return -1;
int l = 0, r = nums.length - 1;
while (l < r) {
int m = l + (r - l) / 2;
if (nums[m] >= nums[l]) {
if (target <= nums[m] && target >= nums[l]) r = m;
else l = m + 1;
}else {
if (target > nums[m] && target <= nums[r]) l = m + 1;
else r = m;
}
}
return nums[l] == target ? l : -1;
}
}
LeetCode OJ 33. Search in Rotated Sorted Array的更多相关文章
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- 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 ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 【一天一道LeetCode】#33. Search in Rotated Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...
- leetcode problem 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 migh ...
- LeetCode OJ: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 migh ...
- 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】33. Search in Rotated Sorted Array
Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...
随机推荐
- 分布式缓存Memcached/memcached/memcache详解及区别
先来解释下标题中的三种写法:首字母大写的Memcached,指的是Memcached服务器,就是独立运行Memcached的后台服务器,用于存储缓存数据的“容器”.memcached和memcache ...
- 洞穴勘测(bzoj 2049)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- 2016NOMS全国运营峰会——史上更强嘉宾阵容提前揭晓!
参加2016NOMS全国运营峰会的演讲嘉宾来自运营领域的各个方面,包括用户运营.内容运营.活动运营.数据运营等.自大会消息一出立刻受到业界的广泛关注,并吸引了众多业内人士踊跃报名.日前,这一运营界峰会 ...
- Linq 内联左联等
我们在做SQL查询的时候经常会用到Inner Join,Left Join,笛卡尔积等等,连接方式的概念方面我想也不用给予太多解释, 我们今天的重点是让大家熟悉LINQ是如何使用Join来实现常用的表 ...
- HTTP状态码(HTTPStatusCode)
HTTP状态码(HTTPStatusCode) 祓焘铺 布稍酡 盛坭馆 距熏屿砥 女装出来扔了套到床上然后自己穿了套 跎徨鼻卩 权术埭 悌颞 蔹咽诹ㄒ 椿酣漂作 钱是小事关键是没有老师耸了 ...
- centos7 crontab笔记
1.crontab相关命令 语法:crontab [-u <用户名称>][配置文件] 或 crontab [-u <用户名称>][-elr] 参数: -e 编辑该用户的计时器设 ...
- 从补丁到POC CVE-2015-0003(2015.3)
从补丁到POC CVE-2015-0003 1. 简介 该漏洞是由于Windows的win32k.sys模块存在对用户层参数验证不完全,导致存在空指针解引用(Null Pointer Derefere ...
- stm32 串口乱码的解决
版权声明:本文为博主原创文章. 前几天在中移物联网申请了一个迷你开发板,运行官方提供的程序,感觉板子是正常的.但是自己写的程序能够刷到板子上,但是串口却是乱码.官方和我的额程序都是用的库函数的方式写的 ...
- 旋转数组中的最小数字,剑指offer,P70 二分查找来实现O(logn)的查找
public class MinNumberInRotatedArray { public int getMinNumInRotatedArray(int[] array) { if(array == ...
- ES6 之 Set数据结构和Map数据结构 Iterator和for...of循环
ECMAScript 6 入门 Set数据结构 基本用法 ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身是一个构造函数,用来生成Set数据结构. va ...