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

  1. class Solution {
  2. public:
  3.  
  4. int search_2(vector<int>& A, int L, int R, int target)
  5. {
  6. while(L<=R)
  7. {
  8. int mid=(L+R)>>;
  9. if(A[mid]>target)
  10. {
  11. R=mid-;
  12. }
  13. else if(A[mid]<target)
  14. {
  15. L=mid+;
  16. }
  17. else return mid;
  18. }
  19. return -;
  20. }
  21.  
  22. int search(vector<int>& nums, int target) {
  23. int n=nums.size();
  24. vector<int> A(nums);
  25. if(n == ) return -;
  26. if(n == )
  27. {
  28. if(A[] == target) return ;
  29. else return -;
  30. }
  31. if(n == )
  32. {
  33. if(A[] == target) return ;
  34. else if(A[] == target) return ;
  35. else return -;
  36. }
  37. int L=,R=n-;
  38. while(L<R)//[0,n-2]找一个比A[n-1]大的数
  39. {
  40. int mid=(L+R>>)+;
  41. if(A[mid]>A[n-]) L=mid;
  42. else R=mid-;
  43. }
  44. int split=L;
  45. if(A[L]<A[n-]) split=n-;
  46. //[0,split],[split+1,n-1]
  47. int ans;
  48. ans=search_2(A,,split,target);
  49. if(ans!=-) return ans;
  50. if(split+<=n-)
  51. {
  52. return search_2(A,split+,n-,target);
  53. }
  54. return -;
  55. }
  56. };

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.

非常简单:

  1. class Solution {
  2. public:
  3. bool subSearch(vector<int>& nums,int left,int right,int target)
  4. {
  5. if(left>right)
  6. return false;
  7. if(nums[left]==target)
  8. return true;
  9. while(nums[left]==nums[right]&&left<right)
  10. left++;
  11. if(left==right)
  12. {
  13. if(nums[left]==target)
  14. return true;
  15. else
  16. return false;
  17. }
  18. int mid=(left+right)/;
  19. if(nums[left]<nums[right])
  20. {
  21. if(nums[left]>target||nums[right]<target)
  22. return false;
  23. else
  24. {
  25. if(target==nums[mid])
  26. return true;
  27. if(target>nums[mid])
  28. left=mid+;
  29. else
  30. right=mid;
  31. return subSearch(nums,left,right,target);
  32. }
  33.  
  34. }
  35. else
  36. return subSearch(nums,left,mid,target)|| subSearch(nums,mid+,right,target);
  37. }
  38. bool search(vector<int>& nums, int target) {
  39. int left=;
  40. int right=nums.size()-;
  41. return subSearch(nums,left,right,target);
  42. }
  43. };

  

Search in Rotated Sorted Array I&&II——二分法的更多相关文章

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

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

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

  4. 33. Search in Rotated Sorted Array旋转数组二分法查询

    一句话思路:反正只是寻找一个最小区间,断开也能二分.根据m第一次的落点,来分情况讨论. 一刷报错: 结构上有根本性错误:应该是while里面包括if,不然会把代码重复写两遍,不好. //situati ...

  5. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  6. 【leetcode】Search in Rotated Sorted Array II

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

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

  8. [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 ...

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

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

随机推荐

  1. C/C++中二维数组和指针关系分析

    在C/c++中,数组和指针有着密切的关系,有很多地方说数组就是指针式错误的一种说法.这两者是不同的数据结构.其实,在C/c++中没有所谓的二维数组,书面表达就是数组的数组.我猜想是为了表述方便才叫它二 ...

  2. tomcat7 access log设置

    位置:${tomcat_home}/conf/server.xml <Valve className="org.apache.catalina.valves.AccessLogValv ...

  3. POJ 3111 二分

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 10507   Accepted: 2709 Case Time ...

  4. matlab求一个矩阵中各元素出现的个数(归一化)

    function [m,n] = stamatrix(a) %网上找到的方法,感觉很巧妙 x=a(:); x=sort(x); d=diff([x;max(x)+1]); count = diff(f ...

  5. Activiti工作流——流程表数据转化

    任务流程部署:  启动流程实例: 请假人完成请假申请: 部门经理完成审批: 总经理审批完成:

  6. vue2路由之指定滑动位置scrollBehavior

    看源码的时候看到这个属性: 新手自然不知道这个是什么东西了,查了下vue  API: https://router.vuejs.org/en/advanced/scroll-behavior.html ...

  7. mpvue开发小记

    1.组件嵌套组件时,子组件作用域bug 组件A内的slot包含子组件B的话,无法正常使用变量(这种情况下,B组件的template错误地使用了A的作用域). 我的解决方案:减少一层组件提炼,即这种情况 ...

  8. HEOI 2012 旅行问题

    2746: [HEOI2012]旅行问题 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1009  Solved: 318[Submit][Statu ...

  9. 【BZOJ】2200: [Usaco2011 Jan]道路和航线

    [题意]给定n个点的图,正权无向边,正负权有向边,保证对有向边(u,v),v无法到达u,求起点出发到达所有点的最短距离. [算法]拓扑排序+dijkstra [题解]因为有负权边,直接对原图进行spf ...

  10. 【NOIP】提高组2015 运输计划

    [题意]n个点的树,m条链,求将一条边的权值置为0使得最大链长最小. [算法]二分+树上差分 [题解] 最大值最小化问题,先考虑二分最大链长. 对所有链长>mid的链整体+1(树上差分). 然后 ...