62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array【medium】
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.
For [4, 5, 1, 2, 3]
and target=1
, return 2
.
For [4, 5, 1, 2, 3]
and target=0
, return -1
.
O(logN) time
错误解法:
class Solution {
public:
/*
* @param A: an integer rotated sorted array
* @param target: an integer to be searched
* @return: an integer
*/
int search(vector<int> A, int target) {
if (A.size() == ) {
return -;
} int start = ;
int end = A.size() - ; while (start + < end) {
int mid = start + (end - start) / ; if (A[mid] == target) {
return mid;
}
else if (A[mid] < target) {
// 5 6 1 2 3 4
if (A[mid] > A[start]) {
start = mid;
}
// 5 6 1 2 3 4
else { // mid < target && mid <= start
end = mid;
}
}
else if (A[mid] > target) { if (A[mid] < A[end]) { // mid > target && mid < end
end = mid;
}
// 3 4 5 6 1 2 找3
else { // mid > target && mid >= end
start = mid;
}
}
} if (A[start] == target) {
return start;
} if (A[end] == target) {
return end;
} return -;
}
};
一开始思考的方向就不对,搞晕了……
解法一:
class Solution {
public:
/*
* @param A: an integer rotated sorted array
* @param target: an integer to be searched
* @return: an integer
*/
int search(vector<int> A, int target) {
if (A.size() == ) {
return -;
} int start = ;
int end = A.size() - ; while (start + < end) {
int mid = start + (end - start) / ; if (A[mid] == target) {
return mid;
} if (A[mid] >= A[start]) {
if (A[start] <= target && target <= A[mid]) {
end = mid;
}
else {
start = mid;
}
}
else {
if (A[mid] <= target && target <= A[end]) {
start = mid;
}
else {
end = mid;
}
}
} if (A[start] == target) {
return start;
} if (A[end] == target) {
return end;
} return -;
}
};
借鉴网上的一个图,可以清楚的归纳一下思路,那么代码就好写了。
这个图参考了:http://fisherlei.blogspot.com/2013/01/leetcode-search-in-rotated-sorted-array.html
解法二:
class Solution {
public:
int search(int A[], int n, int target) {
return searchRotatedSortedArray(A, , n-, target);
} int searchRotatedSortedArray(int A[], int start, int end, int target) {
if(start>end) return -;
int mid = start + (end-start)/;
if(A[mid]==target) return mid; if(A[mid]<A[end]) { // right half sorted
if(target>A[mid] && target<=A[end])
return searchRotatedSortedArray(A, mid+, end, target);
else
return searchRotatedSortedArray(A, start, mid-, target);
}
else { // left half sorted
if(target>=A[start] && target<A[mid])
return searchRotatedSortedArray(A, start, mid-, target);
else
return searchRotatedSortedArray(A, mid+, end, target);
}
}
};
解法三:
class Solution {
public:
int search(int A[], int n, int target) {
int start = , end = n-;
while(start<=end) {
int mid = start + (end-start)/;
if(A[mid]==target) return mid; if(A[mid]<A[end]) { // right half sorted
if(target>A[mid] && target<=A[end])
start = mid+;
else
end = mid-;
}
else { // left half sorted
if(target>=A[start] && target<A[mid])
end = mid-;
else
start = mid+;
}
}
return -;
}
};
解法二和解法三参考了:http://bangbingsyb.blogspot.com/2014/11/leetcode-search-in-rotated-sorted-array.html
思路如下:
题目一看就知道是binary search。所以关键点在于每次要能判断出target位于左半还是右半序列。解这题得先在纸上写几个rotated sorted array的例子出来找下规律。Rotated sorted array根据旋转得多少有两种情况:
原数组:0 1 2 4 5 6 7
情况1: 6 7 0 1 2 4 起始元素0在中间元素的左边
情况2: 2 4 5 6 7 0 起始元素0在中间元素的右边
两种情况都有半边是完全sorted的。根据这半边,当target != A[mid]时,可以分情况判断:
当A[mid] < A[end] < A[start]:情况1,右半序列A[mid+1 : end] sorted
A[mid] < target <= A[end], 右半序列,否则为左半序列。
当A[mid] > A[start] > A[end]:情况2,左半序列A[start : mid-1] sorted
A[start] <= target < A[mid], 左半序列,否则为右半序列
最后总结出:
A[mid] = target, 返回mid,否则
(1) A[mid] < A[end]: A[mid+1 : end] sorted
A[mid] < target <= A[end] 右半,否则左半。
(2) A[mid] > A[end] : A[start : mid-1] sorted
A[start] <= target < A[mid] 左半,否则右半。
62. Search in Rotated Sorted Array【medium】的更多相关文章
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- LeetCode:33. Search in Rotated Sorted Array(Medium)
1. 原题链接 https://leetcode.com/problems/search-in-rotated-sorted-array/description/ 2. 题目要求 给定一个按升序排列的 ...
- [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】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【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】81. Search in Rotated Sorted Array II
Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
随机推荐
- 洛谷 P4173 残缺的字符串
(不知道xjb KMP可不可以做的说) (假设下标都以0开头) 对于有一定偏移量的序列的 对应位置 匹配或者数值计算的题,这里是有一种套路的,就是把其中一个序列翻转过来,然后卷积一下,所得到的新序列C ...
- 【动态规划】bzoj1613 [Usaco2007 Jan]Running贝茜的晨练计划
#include<cstdio> #include<algorithm> using namespace std; #define N 10001 int n,m,a[N],f ...
- python3-os模块中的os.walk(目录树生成器)
#先看源码 def walk(top, topdown=True, onerror=None, followlinks=False): """Directory tree ...
- mysql replication 复制的一些问题
1 过大的复制延迟 mysql 的复制延迟是一个常见问题,现在已经有一些解决方案,如淘宝开发的一些工具 2 没有磁盘空间 复制导致磁盘空间塞满,二进制日志.中继日志或临时文件把磁盘塞满,slave ...
- css活用,评分点击星星
1.字符图集 2.css样式 .cleanfloat::after{display: block; clear: both; content:""; visibility: hid ...
- mysql 初始化报错 /usr/local/mysql/bin/mysqld:error while loading shared libraries :libaio.so.1
安装mysql在初始化的时候,出现/usr/local/mysql/bin/mysqld:error while loading shared libraries:libaio.so.1 :canno ...
- Flask+uwsgi+Nginx环境搭建
开源软件准备需要的软件列表:setuptools-33.1.1.zipPython-2.7.13.tgzpip-9.0.1.tar.gznginx-1.10.3.tar.gz软件统一上传到/usr/l ...
- javascript快速入门14--DOM基础
DOM(Document Object Model)——文档对象模型 什么是DOM? Document Object Model (DOM)是HTML和XML文档的编程接口.它提供了上述文档的一种结构 ...
- Sqoop操作实践
Sqoop操作实践 @(Hadoop) Sqoop常用参命令 序号 命令/command 类 说明 1 impor ImportTool 从关系型数据库中导入数据(来自表或者查询语句)到HDFS中 2 ...
- EasyUI-DataGrid动态合并单元格
js /** * EasyUI DataGrid根据字段动态合并单元格 * @param fldList 要合并table的id * @param fldList 要合并的列,用逗号分隔(例如:&qu ...