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.

Have you met this question in a real interview?

Yes
Example

For [4, 5, 1, 2, 3] and target=1, return 2.

For [4, 5, 1, 2, 3] and target=0, return -1.

Challenge

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】的更多相关文章

  1. 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 ...

  2. LeetCode:33. Search in Rotated Sorted Array(Medium)

    1. 原题链接 https://leetcode.com/problems/search-in-rotated-sorted-array/description/ 2. 题目要求 给定一个按升序排列的 ...

  3. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  4. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  5. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  6. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  7. 【leetcode】Search in Rotated Sorted Array II(middle)☆

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  8. 【Leetcode】81. Search in Rotated Sorted Array II

    Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...

  9. 【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 ...

随机推荐

  1. [BZOJ 1801] Chess

    Link: BZOJ 1801 传送门 Solution: 一眼看过去又像是状压$dp$的经典模型…… 但此题$n,m\le 100$ ,直接跑状压只有50分 此时要发现这道题的特点:每行/列不能放置 ...

  2. 【拓扑排序】CODEVS 2833 奇怪的梦境

    拓扑排序模板. #include<cstdio> #include<vector> #include<stack> using namespace std; #de ...

  3. [Gym101194G][CHINA-Final2016]Pandaria

    题目大意: 给你一个$n(n\le10^5)$个点,$m(m\le2\times10^5)$条边的无向图,每个点有一个颜色$c_i$,每条边有一个边权$w_i$.$q(q\le2\times10^5) ...

  4. 【JVM】Myecplise自带的JVM大小调整,用于Junit等测试时使用

    一般在使用Junit或者一个工具类的main方法执行时,在Myecplise中运行,并不会占用多大的堆空间.如果出现OutofMemory错误,调整MyEcplise自带的JVM大小. 在Myecpl ...

  5. coco2dx jni 调用 java 相机返回 图片数据

    新建 一个项目 名字:testJin  包名:com.TanSon.org  python命令:python create_project.py -project testJin -package c ...

  6. Git -- 自己项目关联新建的git

  7. [Ubuntu Setup] Ubuntu 14.10 LTS 中文输入法的安装

    from : http://www.cnblogs.com/zhj5chengfeng/archive/2013/06/23/3150620.html http://xboot.org/thread- ...

  8. WebLogic Server 12.1.2后的字符型安装模式

    weblogic Server 12.1.1全部都可以用原来方式. WebLogic Server 12.1.2后已经取消了console安装模式,目前只有gui和静默安装模式.并且安装方式下也有很大 ...

  9. csharp 面向对象编程

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shap ...

  10. 顶点缓存对象(VBO)【转】

    http://www.cnblogs.com/hefee/p/3824300.html 顶点缓存对象(VBO) 创建VBO 绘制VBO 更新VBO 实例 GL_ARB_vertex_buffer_ob ...