LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
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.
我突然想用英文解说下,展示一下我英文解说这么专业主题的功力O(∩_∩)O~,都搞双语的话好像又太费时间了。
This is a classic interview question. It's solution is similar with normal binary search. The only difference is that we need to add more conditional sentences.
和普通的二分法差不多
The key points is when we divide the array into two half, we need to compare A[mid] with one of the end element of the array, so that we can know which half of the array is sorted, and which half is rotated, and divided them again subsequently.
关键是会增加条件判断
The difference I do here is that I add one normal binary search here as a helper function. Actually you don't need the binary search function, just one function will be alright. But that's fun to add them together to check the difference between them.
增加普通的binary search对比一下
class Solution {
public:
int search(int A[], int n, int target)
{
return unordBiSearch(A, 0, n-1, target);
}
int unordBiSearch(int A[], int low, int up, int tar)
{
if (low > up) return -1;
int mid = (low+up)>>1;
if (A[mid] == tar)
return mid;
if (A[mid]>A[up])
{
if (A[low] <= tar && A[mid] > tar)
return biSearch(A, low, mid-1, tar);
else return unordBiSearch(A, mid+1, up, tar);
}
if (A[mid]<A[up])
{
if (A[mid] < tar && A[up] >= tar)
return biSearch(A, mid+1, up, tar);
else return unordBiSearch(A, low, mid-1, tar);
}
return -1;
}
int biSearch(int A[], int low, int up, int key)
{
if(low>up) return -1;
int mid = (low+up)>>1;
if (key < A[mid])
return biSearch(A, low, mid-1, key);
else if (A[mid] < key)
return biSearch(A, mid+1, up, key);
return mid;
}
};
LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找的更多相关文章
- LeetCode OJ: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旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- [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 7might ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...
- [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 搜索旋转排序数组 python实现
题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组 中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...
- [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 ...
随机推荐
- php5.3 不支持 session_register() 此函数已启用的解决方法
php从5.2.x升级到5.3.2.出来问题了.有些原来能用的程序报错了,Deprecated: Function session_register() is deprecated php从5.2.x ...
- App - 版本控制
/** 版本判断 ***/ NSString *versionKey = @"CFBundleVersion"; // 上一次使用版本号(存储在沙盒中的版本号) NSString ...
- 织梦中limit的用法详解(调用指定id下的指定文章)
limit的用法大致可以理解为:调用指定id下的指定文章. 下面为代码片段,需要的朋友自行拿去: {dede:arclist typeid='6' row='1' limit='0,1'} <l ...
- html页面高度不同浏览器兼容性设置
页面需要嵌套在跨域的iframe中,而页面高度不固定,需要每个页面把自己的高度获得后,通过js通知iframe调整显示. 而页面在获得自己的高度时,发现总是比预想的大.经过参考别人的博客,发现原来是w ...
- client denied by server configuration
http://blog.csdn.net/fdipzone/article/details/40512229
- 《鸟哥的Linux私房菜》读书笔记四
1.Linux的目录配置以『树状目录』来配置,至於磁碟分割槽(partition)则需要与树状目录相配合! 请问,在预设的情况下,在安装的时候系统会要求你一定要分割出来的两个Partition为何? ...
- logstash date插件
[elk@dr-mysql01 api-access]$ date Wed Nov 30 19:21:35 CST 2016 [elk@dr-mysql01 api-access]$ [elk@dr- ...
- android-86-Can't create handler inside thread that has not called Looper.prepare()
以下是Android API中的一个典型的Looper thread实现: //Handler不带参数的默认构造函数:new Handler(),实际上是通过Looper.myLooper()来获取当 ...
- Android事件分发详解(三)——ViewGroup的dispatchTouchEvent()源码学习
package cc.aa; import android.os.Environment; import android.view.MotionEvent; import android.view.V ...
- easyui-lang-zh_CN.js导入后还是英文提示
<script src="/js/easyUI1.3.3/jquery.easyui.min.js" type="text/javascript"> ...