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.
题目标签:Array
Java Solution:
Runtime beats 70.31%
完成日期:07/14/2017
关键词:Array
关键点:利用Binary Search 结合 rotated sorted array 中必然有一半是有序序列 来搜索
public class Solution
{
public int search(int[] nums, int target)
{
if(nums == null || nums.length == 0)
return -1; int left = 0;
int right = nums.length - 1; while(left <= right)
{
int mid = left + (right - left) / 2; if(nums[mid] == target) // if the middle one is target, return mid index
return mid;
else if(nums[mid] >= nums[left]) // meaning left half is ascending order
{
if(target >= nums[left] && target < nums[mid]) // if target is in left half
right = mid - 1; // move to left half to search
else // target is in right half
left = mid + 1; // move to right half to search
}
else // meaning right half is ascending order
{
if(target > nums[mid] && target <= nums[right]) // if target is in right half
left = mid + 1;
else // target is in left half
right = mid - 1; } } return -1;
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/4325648.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)的更多相关文章
- [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. ...
- [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索
11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code ...
- [LeetCode] 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(循环有序数组中进行查找操作)
题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description Problem :当前的数组 ...
- leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- [LeetCode] Find Minimum 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 ...
随机推荐
- iOS内购 服务端票据验证及漏单引发的思考.
因业务需要实现了APP内购处理,但在过程中出现了部分不可控的因素,导致部分用户反映有充值不成并漏单的情况. 仔细考虑了几个付费安全上的问题,凡是涉及到付费的问题都很敏感,任何一方出现损失都是不能接受的 ...
- 关于APP在小米5s第一次安装启动后,点击home返回桌面,再次进入重进闪屏页问题
现象 今天工作中,在对公司产品进行测试的时候,程序员小哥点出了一个问题.问题点出的步骤是这样的: 1.安装APP 2.点击打开 3.经过闪屏页,进入主页后,点击HOME键 4.再次进入程序会重新进入闪 ...
- 才趟过的一个坑,css造成的Validform表单提交按钮点击无效
最近入手的一个项目,在开发的过程中,遇到了一个以前没遇到过的问题,废了半天的功夫才弄懂原因,留下足迹,警醒后人,下面开始讲故事啦! 在一个昏天暗地的上午,我一个人照常坐在办公室安静的工作中!项目编码已 ...
- python列表补充、循环
优先掌握部分 切片l=['a','b','c','d','e','f']print(l[1:5])print(l[1:5:2])print(l[2:5])print(l[-1])了解print(l[- ...
- PowerDesigner连接MySQL和逆向工程图
0.写在前面的话 最近想梳理公司项目的表间关系,从项目后台管理系统的操作入手,以及代码的hibernate注解入手,都不算特别尽人意,于是最后还是鼓捣了一下PowerDesigner的逆向工程图,这样 ...
- JVM菜鸟进阶高手之路三
转载请注明原创出处,谢谢! 笨神大大分享: 小程序里面搜索:JVMPocket,这个小程序是笨神大大提供的,里面可以搜索相关JVM参数,用法. -XX:MaxTenuringThreshold,这个参 ...
- Centos6.7安装chrome
cd /etc/yum.repos.dwget http://people.centos.org/hughesjr/chromium/6/chromium-el6.repo yum install c ...
- css常用属性1
1 背景相关 背景颜色 background-color = 颜色名称/rgb值/十六进制值 背景图片 background-image = url('') 背景图片平铺方式 backgro ...
- ASP.NET Core中如何调整HTTP请求大小的几种方式
一.前言 一般的情况下,我们都无需调用HTTP请求的大小,只有在上传一些大文件,或者使用HTTP协议写入较大的值时(如调用WebService)才可能会调用HTTP最大请求值. 在ASP.NET Co ...
- YYHS-NOIP2017Training0928-ZCC loves Isaac
题目描述 [背景]ZCC又在打Isaac.这次他打通了宝箱关进入了表箱关.[题目描述]表箱关有一个房间非常可怕,它由n个变异天启组成.每个天启都会在进入房间后吐出绿弹并炸向某一个位置且范围内只有一个天 ...