循环队列搜索 Search in Rotated Sorted Array
这里比较重要的是,不要一上来就判断mid 和 target有没有关系。
因为数组是无序的,这样的判断毫无结论,只会搞的更复杂。
应该先想办法判断出哪一侧是有序的。 class Solution {
public:
int search(vector<int>& a, int target) {
int low, high, mid; low = , high = a.size(); while(low < high)
{
mid = low + (high - low)/; if(a[mid] == target) return mid; if(a[low] < a[mid]) //left is in order
{
if(target >= a[low] && target < a[mid]) //target in left side
{
high = mid;
}
else
{
low = mid + ;
}
}
else // right in order
{
if(target > a[mid] && target < a[low])
{
low = mid + ;
}
else
{
high = mid;
}
}
} return -;
}
};
循环队列搜索 Search in Rotated Sorted Array的更多相关文章
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
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] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [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 81. 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(在旋转有序序列中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [Swift]LeetCode81. 搜索旋转排序数组 II | Search in Rotated Sorted Array II
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
随机推荐
- Docker系列(三)Dockerfile 离线构建镜像
一.Dockfile介绍: Dockfile是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令.Docker程序将这些Dockerfil ...
- centos7配置iscsi
什么是ISCSI iscsi--internet small computer system interface互联小型计算机系统接口,将数据包封装在TCP/IP协议中传输,使用普通网线和网络设备即可 ...
- 详解java定时任务---Timer篇
一.简介 在java的jdk中提供了Timer.TimerTask两个类来做定时任务. Timer是一种定时器工具,用来在一个后台线程计划执行指定任务,而TimerTask一个抽象类,它的子 ...
- pascalcontext-fcn全卷积网络结构理解
一.说明 fcn的开源代码:https://github.com/shelhamer/fcn.berkeleyvision.org 论文地址:fully convolutional networks ...
- 什么是HDR?
参考:https://baijiahao.baidu.com/s?id=1606763887374415267&wfr=spider&for=pc HDR——即高动态范围图像(High ...
- Java Swing 界面中文乱码问题解决(Idea环境)
编译(build)的时候和运行的时候使用同样的字符集就可以了.这里,我都设置为“UTF-8”.具体做当如下: File->Settings->Build...->Compiler,在 ...
- SQL脚本--总耗CPU最多的前个SQL --平均耗CPU最多的前个SQL
--总耗CPU最多的前个SQL SELECT TOP 20 total_worker_time/1000 AS [总消耗CPU 时间(ms)],execution_count [运行次数], qs.t ...
- transform、transition、animation 区别
Transform 在部分的test case当中,每每演示transform属性的,看起来好像都是带动画.这使得小部分直觉化思维的人(包括我)认为transform属性是动画属性.而恰恰相反,tra ...
- Redis配置不当致使root被提权漏洞
Redis配置不当致使root被提权漏洞 Dear all~ 最近Redis服务被曝出因配置不当,可能造成数据库被恶意清空,或被黑客利用写入后门文件造成进一步入侵,请关注! 一.漏洞发布日期 2015 ...
- php中cookie和session的总结
cookie: 设置cookie: setcookie("name","zhang","time()+3600"); 参数一:属性名 参数 ...