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 ...
随机推荐
- FastCgi与PHP-fpm关系[转] 读完本文瞬间明朗了很多
刚开始对这个问题我也挺纠结的,看了<HTTP权威指南>后,感觉清晰了不少. 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. ...
- python之全栈开发——————IO模型
一:在讲IO模型之前我们首先来讲一下事件驱动模型,属于一种编程的范式,那么我们以前就是传统式编程,来看看有什么区别吧(此处为借鉴别人的) 传统的编程是如下线性模式的: 开始--->代码块A--- ...
- 推送 -- error:Not get deviceToken yet
获取不到devicetoken请检查: 1) 请检查你的pushconfig里的appkey与应用详情中是否一致 2) bundleid有3处需要检查是否一致:a.你的应用详情中的bu ...
- bzoj 2251: [2010Beijing Wc]外星联络 后缀数组
2251: [2010Beijing Wc]外星联络 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 424 Solved: 232[Submit][ ...
- 【2011 Greater New York Regional 】Problem G: Rancher's Gift
计算几何的题目,很简单: 自己随手敲了个,纪念下! #include<cstdio> #include<cmath> using namespace std; struct p ...
- C语言中的volatile
转自C语言的那些小秘密之volatile volatile的重要性对于搞嵌入式的程序员来说是不言而喻的,对于volatile的了解程度常常被不少公司在招聘嵌入式编程人员面试的时候作为衡量一个应聘者是否 ...
- vs2015 Xamarin.Android安装
原文:vs2015 Xamarin.Android安装 Xamarin.Android 安装步骤,以vs2015为例 1,安装vs2015中的跨平台项,但是安装在国内肯定失败,因为需要到谷歌下载 当我 ...
- 监控持有sql和被堵塞的sql
Session 1: mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update Client ...
- 17.2.2.1 The Slave Relay Log Slave中继日志
17.2.2.1 The Slave Relay Log Slave中继日志 中继日志, 像binary log,有一组文件组成包含events 描述数据库的修改,和一个index文件包含所有使用过的 ...
- 【HDOJ】2133 What day is it
需要注意数据有效性. #include <stdio.h> #define isLeapYear(y) (y%4==0&&y%100!=0)||(y%400==0) ][] ...