LeetCode OJ: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.
相当于将数组的后面一部折叠到数组的前面去了,本质上也是二分法,这里其实也是有规律可循的:首先要得到转折点,也就是其左边右边都比其大的那一点。给一个start以及一个end,如果nums[mid] > nums[start],那么说明从start到mid也一定是递增的,很容的知道pivot一定在mid+1到end之间,那么递归的去查找就可以了。nums[mid] < nums[start]可以同样的去分析,代码如下所示:
- class Solution {
- public:
- int search(vector<int>& nums, int target) {
- int pivot = getPivot(nums, , nums.size() - );
- int pos = bSearch(nums, , pivot - , target);
- if(pos != -)
- return pos;
- return bSearch(nums, pivot, nums.size() - , target);
- }
- int getPivot(vector<int>&nums, int start, int end){
- if(start > end)
- return -;
- int mid = start + (end - start)/;
- if(nums[start] <= nums[mid]){
- int pos = getPivot(nums, mid + , end);
- if(pos == -) return start;
- else
- if(nums[pos] < nums[start])
- return pos;
- else
- return start;
- }else{
- int pos = getPivot(nums, start, mid);
- if(pos == -)
- return mid;
- if(nums[pos] < nums[end])
- return pos;
- else
- return mid;
- }
- }
- int bSearch(vector<int>&nums, int start, int end, int target){
- int mid;
- while(start <= end){
- mid = (start+end)/;
- if(nums[mid] > target){
- end = mid - ;
- }else if(nums[mid] < target){
- start = mid + ;
- }else{
- return mid;
- }
- }
- return -; //返回-1,表明在这一部分没有找到,可以在下一部分查找
- }
- };
LeetCode OJ:Search in Rotated Sorted Array(翻转排序数组的查找)的更多相关文章
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- [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 [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [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. 思路 ...
- Java for LeetCode 081 Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【LeetCode】Search in Rotated Sorted Array II(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
- Leetcode系列-Search in Rotated Sorted Array
做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- 简单介绍--TOSCA自动化测试工具
1.工具源自 TOSCA由公司Tricentis研发.这是一家来自奥地利的软件初创企业,专门帮助企业开发团队进行自动化软件测试.(百科) TOSCA的思想是,不用会编程的测试人员可以直接上手自动化. ...
- .NET BETWEEN方法
Between 值范围比较 可以判断一个值是否落在区间范围值中. public static bool Between<T>(this T me, T lower, T upper) wh ...
- python 打印 九九表
用Python 打印九九表. print 每打印一行默认会带有换行, 在print语句后加上,end = 't' 会变成tab. 排版会好点. def main(): for i in range(1 ...
- RabbitMQ的安装及集群搭建方法
RabbitMQ安装 1 安装erlang 下载地址:http://www.erlang.org/downloads 博主这里采用的是otp_src_19.1.tar.gz (200MB+) [roo ...
- 20145322第四周JAVA程序设计基础学习总结
20145322 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 一.继承共同行为 何谓继承 1 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再 ...
- 20145240《网络对抗》Web安全基础实践
Web安全基础实践 实验后回答问题 (1)SQL注入攻击原理,如何防御 原理:SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语 ...
- uboot的FIT功能
1.FIT介绍 Flattend Image Tree 英文原版介绍: FIT is formally a FDT, which can include images of various types ...
- LeetCode——Is Subsequence
Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...
- centos7 firewall 操作
一.firewall配置 The configuration for firewalld is stored in various XML files in /usr/lib/firewalld/ a ...
- 【医学图像】3D Deep Leaky Noisy-or Network 论文阅读(转)
文章来源:https://blog.csdn.net/u013058162/article/details/80470426 3D Deep Leaky Noisy-or Network 论文阅读 原 ...