Search in Rotated Sorted Array I&&II——二分法
Search in Rotated Sorted Array I
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.
做完了Find Minimum in Rotated Sorted Array之后,对这题又发现了一种非常简单的做法。
Find Minimum in Rotated Sorted Array的思路如下:
当nums[left]<nums[right],说明数组没有旋转,是按升序排列的。可直接返回nums[left];
当nums[left]<nums[mid],说明left至mid这段是按升序排列的,可令left=mid+1;
当nums[left]>nums[mid],说明mid至right这段是按升序排列的,可令right=mid;
那么这题的解法也就出来了,把数组分成两部分,那肯定是有一部分是有序的,另一部分是无序的,不管有序还是无序,都对其继续进行二分搜索,最终肯定都能得到有序的数组,不过这样做感觉就不是二分搜索了,因为每次并没有去掉一半。
也是通过二分搜索来解决,先通过一个二分搜索找到旋转的点,再分别对前后两个有序数组使用二分搜索,思路很简单,代码也没自己写了。
转:http://blog.csdn.net/zhangwei1120112119/article/details/16829309
- class Solution {
- public:
- int search_2(vector<int>& A, int L, int R, int target)
- {
- while(L<=R)
- {
- int mid=(L+R)>>;
- if(A[mid]>target)
- {
- R=mid-;
- }
- else if(A[mid]<target)
- {
- L=mid+;
- }
- else return mid;
- }
- return -;
- }
- int search(vector<int>& nums, int target) {
- int n=nums.size();
- vector<int> A(nums);
- if(n == ) return -;
- if(n == )
- {
- if(A[] == target) return ;
- else return -;
- }
- if(n == )
- {
- if(A[] == target) return ;
- else if(A[] == target) return ;
- else return -;
- }
- int L=,R=n-;
- while(L<R)//[0,n-2]找一个比A[n-1]大的数
- {
- int mid=(L+R>>)+;
- if(A[mid]>A[n-]) L=mid;
- else R=mid-;
- }
- int split=L;
- if(A[L]<A[n-]) split=n-;
- //[0,split],[split+1,n-1]
- int ans;
- ans=search_2(A,,split,target);
- if(ans!=-) return ans;
- if(split+<=n-)
- {
- return search_2(A,split+,n-,target);
- }
- return -;
- }
- };
Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
非常简单:
- class Solution {
- public:
- bool subSearch(vector<int>& nums,int left,int right,int target)
- {
- if(left>right)
- return false;
- if(nums[left]==target)
- return true;
- while(nums[left]==nums[right]&&left<right)
- left++;
- if(left==right)
- {
- if(nums[left]==target)
- return true;
- else
- return false;
- }
- int mid=(left+right)/;
- if(nums[left]<nums[right])
- {
- if(nums[left]>target||nums[right]<target)
- return false;
- else
- {
- if(target==nums[mid])
- return true;
- if(target>nums[mid])
- left=mid+;
- else
- right=mid;
- return subSearch(nums,left,right,target);
- }
- }
- else
- return subSearch(nums,left,mid,target)|| subSearch(nums,mid+,right,target);
- }
- bool search(vector<int>& nums, int target) {
- int left=;
- int right=nums.size()-;
- return subSearch(nums,left,right,target);
- }
- };
Search in Rotated Sorted Array I&&II——二分法的更多相关文章
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- Search in Rotated Sorted Array I II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- Search in Rotated Sorted Array (I, II) 解答
Question Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 ...
- 33. Search in Rotated Sorted Array旋转数组二分法查询
一句话思路:反正只是寻找一个最小区间,断开也能二分.根据m第一次的落点,来分情况讨论. 一刷报错: 结构上有根本性错误:应该是while里面包括if,不然会把代码重复写两遍,不好. //situati ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- C/C++中二维数组和指针关系分析
在C/c++中,数组和指针有着密切的关系,有很多地方说数组就是指针式错误的一种说法.这两者是不同的数据结构.其实,在C/c++中没有所谓的二维数组,书面表达就是数组的数组.我猜想是为了表述方便才叫它二 ...
- tomcat7 access log设置
位置:${tomcat_home}/conf/server.xml <Valve className="org.apache.catalina.valves.AccessLogValv ...
- POJ 3111 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 10507 Accepted: 2709 Case Time ...
- matlab求一个矩阵中各元素出现的个数(归一化)
function [m,n] = stamatrix(a) %网上找到的方法,感觉很巧妙 x=a(:); x=sort(x); d=diff([x;max(x)+1]); count = diff(f ...
- Activiti工作流——流程表数据转化
任务流程部署: 启动流程实例: 请假人完成请假申请: 部门经理完成审批: 总经理审批完成:
- vue2路由之指定滑动位置scrollBehavior
看源码的时候看到这个属性: 新手自然不知道这个是什么东西了,查了下vue API: https://router.vuejs.org/en/advanced/scroll-behavior.html ...
- mpvue开发小记
1.组件嵌套组件时,子组件作用域bug 组件A内的slot包含子组件B的话,无法正常使用变量(这种情况下,B组件的template错误地使用了A的作用域). 我的解决方案:减少一层组件提炼,即这种情况 ...
- HEOI 2012 旅行问题
2746: [HEOI2012]旅行问题 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 1009 Solved: 318[Submit][Statu ...
- 【BZOJ】2200: [Usaco2011 Jan]道路和航线
[题意]给定n个点的图,正权无向边,正负权有向边,保证对有向边(u,v),v无法到达u,求起点出发到达所有点的最短距离. [算法]拓扑排序+dijkstra [题解]因为有负权边,直接对原图进行spf ...
- 【NOIP】提高组2015 运输计划
[题意]n个点的树,m条链,求将一条边的权值置为0使得最大链长最小. [算法]二分+树上差分 [题解] 最大值最小化问题,先考虑二分最大链长. 对所有链长>mid的链整体+1(树上差分). 然后 ...