【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 ...
随机推荐
- EF上下文管理
- 总结——R中查看属性的函数
本文原创,转载注明出处,本人Q1273314690 R中知道一个变量的主要内容和结构,对我们编写代码是很重要的,也可以帮我们避免很多错误. 但是,R中有好几个关于属性查看的函数,我们往往不知道什么时候 ...
- 【Solr】copy字段的应用
目录 界面查询应用 熟悉Schema.xml copy域的应用 回到顶部 界面查询应用 添加一个文档 查询添加的文档 以上详细介绍了query里面的参数详解. 当不输入任何条件时,进行查询,看看返回结 ...
- 71 mac boook pro 无 gpu 下caffe 安装
71 mac boook pro 无 gpu 下caffe 安装 1.首先安装homebrew工具,相当于Mac下的yum或apt ruby -e "$(curl -fsSL https:/ ...
- 使用Carthage安装第三方Swift库
http://blog.crazyphper.com/?p=3661 carthage update
- div+css模仿登录界面
我的代码,这种方式形成了遮罩层,遮罩层的"登录"按钮仍可被点击,非我想要的. <!DOCTYPE html> <html lang="en"& ...
- 如何让网页在浏览器标题栏显示自己制作的图标ico
第一步,制作一个尺寸16x16大小的PNG图片,可以用photoshop等图片处理工具来设计,然后保存到本地电脑上,通过ico在线制作或使用IconWorkshop工具制作ICO图标,ico图标命名为 ...
- nginx反向代理proxy模块相关参数
http_proxy_module Proxy_pass proxy_pass指令属于ngx_http_proxy_module模块,此模块可以将请求转发到另一台服务器:官方说明:http://ngi ...
- storyboard有多个Segue的传递
在项目中需要在一个页面向多个页面传不同的值. 在view2Controller和view3Controller中分别有相应的Str2和Str3 - (void)prepareForSegue:(UIS ...
- VS2012创建UML项目
1.选择建模工具 2.添加新建项 3.添加UML图或用例图 4.打开工具箱添加