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.
ps:这题和剑指offter的题目类似。一次AC
思路,由于没有重复数字,
1.我们先找出最大值在哪个位置。
2.然后拿target和A[0]做比较,若target>A[0],则在0~max_loc之间来二分查找数组;若target<A[0],则在max_loc+1~n-1来查找数组。
代码:
class Solution {
public:
int binarySearch(int start,int end,int A[],int target){
int l=start;int r=end;int mid=;
while(l<=r){
mid=(l+r)/;
if(A[mid]>target) r=mid-;
else if(A[mid]<target) l=mid+;
else return mid;
}
return -;
}
int findMaxValueLoction(int A[],int n){
if(A[n-]>A[]) return n-;
int l=;int r=n-;int mid=;
while(l<=r){
mid=(l+r)/;
if(mid==) return mid;
else if(mid!=&&A[mid]>A[]&&A[mid]>A[mid-]&&A[mid]<A[mid+]){
l=mid+;
}
else if(mid!=&&A[mid]<A[]&&A[mid]>A[mid-]&&A[mid]<A[mid+]){
r=mid-;
}
else if(mid!=&&A[mid]>A[mid-]&&A[mid]>A[mid+]){
return mid;
}else{
return mid-;
}
}
return -;
}
int search(int A[], int n, int target) {
if(A==NULL||n==) return -;
int max_loc=findMaxValueLoction(A,n);
if(target>A[])
return binarySearch(,max_loc,A,target);
else if(target<A[])
return binarySearch(max_loc+,n-,A,target);
else
return ;
}
};
Search in Rotated Sorted Array(二分查找)的更多相关文章
- 33. Search in Rotated Sorted Array(二分查找)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Leetcode 153. Find Minimum in Rotated Sorted Array -- 二分查找的变种
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- 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 ...
- 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 OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- Search in Rotated Sorted Array I II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
随机推荐
- js中json处理总结之JSON.parse
踩过的坑都将成为路上的风景.队友在cookie中已存以下值: address_info {"address_name":"人民大会堂","...lng ...
- 一个PHP开发APP接口的视频教程
感觉php做接口方面的教程很少,无意中搜到了这个视频教程,希望能给一些人带来帮助http://www.imooc.com/learn/163
- 2015Java参赛邀请函
[导读]甲骨文公司值Java语言发布20周年之际,携手全国高等级学校计算机教育研究会.教育部高等学校计算机类专业教学指导委员会,共同举办了2015年“甲骨文杯”全国Java程序设计大赛,为二百万名中国 ...
- QT+信号和槽函数_自定义槽函数_一个信号对应多个槽函数
以下的代码里面有自定义槽函数的内容,同时也有信号实现的函数: #ifndef MAINWIDGET_H #define MAINWIDGET_H #include <QWidget> #i ...
- poj3710 Christmas Game
题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...
- phpstudy里升级mysql版本到5.7
phpstudy里没有地方可以设置mysql数据库,很多人都疑惑在phpstudy里怎么升级mysql数据库版本,本文就教你如何在phpstudy中升级mysql的版本. PhpStudy集成环境中的 ...
- docker快速搭建
curl -sSL https://get.docker.com|sh docker --version systemctl start docker.service ps -ef|grep doc ...
- 【Java】Class文件编译的版本号与JDK版本号的对应关系
查看方式 使用文本编辑器EmEditor以16进制方式打开.class文件 图中红框中的代表版本号52.0 次版本号:00 00 (小数点后面的部分) 主版本号:00 34 (小数点前面的部分 ...
- 如何用纯 CSS 创作一个渐变色动画边框
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/odpRKX 可交互视频教 ...
- mysql优化之参数优化(转)
1.优化方式 硬件优化=>系统优化=>mysql配置优化=>SCHEMA优化=>sql优化=>其他解决方案(redis or MongoDB or Cassandra o ...