【leetcode】Search in Rotated Sorted Array (hard)
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.
思路:二分搜索,每次去掉一半的错误选项。
注意,每次 l = m + 1, r = m - 1 防止无限循环。
int search(int A[], int n, int target) {
int l = , r = n - ;
while(l <= r) //注意有等号
{
int m = (l + r) / ;
if(A[m] == target)
return m;
if(A[l] <= A[m] && A[m] <= A[r]) //顺序的
{
if(A[m] > target)
r = m - ;
else
l = m + ;
}
else if(A[l] >= A[m] && A[m] <= A[r]) //开头转到了左半部分
{
if(A[m] < target && target <= A[r]) //在右半部分
l = m + ;
else
r = m - ;
}
else //开头转到了右半部分
{
if(A[l] <= target && target <= A[m]) //在左半部分
r = m - ;
else
l = m + ;
}
}
return -;
}
大神简约版写法:去掉一半选项时的思路不同
int search(int A[], int n,int target) {
int lo = ;
int hi = n - ;
while (lo <= hi) {
int mid = (lo + hi) / ;
if (A[mid] == target) return mid; if (A[lo] <= A[mid]) {
if (target >= A[lo] && target < A[mid]) {
hi = mid - ;
} else {
lo = mid + ;
}
} else {
if (target > A[mid] && target <= A[hi]) {
lo = mid + ;
} else {
hi = mid - ;
}
}
}
return -;
}
【leetcode】Search in Rotated Sorted Array (hard)的更多相关文章
- 【LeetCode】Search in Rotated Sorted Array II(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【leetcode】Search in Rotated Sorted Array
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【题解】【数组】【查找】【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】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 ...
- 【Leetcode】【Hard】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] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
随机推荐
- linux系统安装yum环境
http://blog.sina.com.cn/s/blog_63d8dad80101cn2s.html 1.卸载rhel的默认安装的yum包 查看yum包 rpm -qa|grep yum 卸载之 ...
- 安装SQL Server 2012 『企业中文版』
安装 SQL Server 前,请详细参阅:计划安装SQL Server2012需求详细http://www.cnblogs.com/chhuang/p/3623198.html 安装 SQL Ser ...
- 真机调试 —— An unknown error occurred.
iOS开发中,总会遇见各种各样的问题.今天我就在真机调试的时候出现 An unknown error occurred. 不知道什么鬼,百度一下,各种胡说八道. 解决办法: 1.退出Xcode,重新运 ...
- 第24天 runtime
面试时被问到一个问题,如何实现weak变量的自动置nil?当时也不知道. 今天在实现target-action模式时,如何调用SEL,刚开始只会PerformSelector,但不能传递多个参数,后来 ...
- Code First01---CodeFirst项目的搭建
Entity Framework支持Database First.Model First和Code Only三种开发模式,各模式的开发流程大相径庭,开发体验完全不一样.三种开发模式各有优缺点,对于程序 ...
- CSU 1120 病毒(DP)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...
- 从Objective-C转战C++ Android平台开发实践(C++/Java)
是否使用虚拟方法 最好在不用“virtual”关键字的情况下声明所有cpp成员方法 但是在写CPP头文件时,请检查有没有父类的方法被当前的工作覆盖.如果有,请确保将这些方法改为虚拟方法. 如果从父类继 ...
- [转载]Jquery mobile 新手问题总汇
原文链接:http://www.wglong.com/main/artical!details?id=4 此文章将会持续更新,主要收录一些新手比较常见的问题. 欢迎 向我推荐比较典型的常见问题,我会记 ...
- 2 GPS utility methods
Methond 1 is to check whether the GPS is on: 1 2 3 4 5 public boolean isGPSIsOn() { LocationManager ...
- Shape + Selector: Make a Shape as one item of the Selector
Generally, I use a selector to select pictures or colors to render the normal and the pressed backgr ...