题目:

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

Find the minimum element.

You may assume no duplicate exists in the array.

解题思路:

首先假设一个sorted没有rotated的数组[1,2,3],假设我们通过一个pivot把这个数组rotate,那么结果可能为:[2,3,1], [3,1,2], 可以发现:num[low]永远大于(或等于)num[high]。因为你之前是sorted的数组,你在一个sorted的数组找了一个pivot进行rotate,那么比如pivot后面的值都大于pivot之前的值。所以依据这个发现,以及二分法查找。我们可以根据以下判断来解题。num[mid]有两种可能性,如果num[mid] > num[high],证明num[mid]在rotated后的那个区间内,这个区间我们刚才已知都大于pivot之前的值,所以最小值就在low=mid+1那个区间内。另一种可能,num[mid] <= num[high],那么我们刚才可以看出来这种可能性说明mid~high以及是排好序的,那么最小值在high=mid这个区间内(mid可能是最小值)。依据此判断可以找到最小值。

代码如下:

 1     public int findMin(int[] num) {
 2         int low = 0, high = num.length - 1;
 3         while (low < high && num[low] >= num[high]) {
 4             int mid = (low + high) / 2;
 5             if (num[mid] > num[high])
 6                 low = mid + 1;
 7             else
 8                 high = mid;
 9         }
         return num[low];
     }

Find Minimum in Rotated Sorted Array leetcode java的更多相关文章

  1. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  2. leetcode 154. Find Minimum in Rotated Sorted Array II --------- java

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. 153. Find Minimum in Rotated Sorted Array - LeetCode

    Question 153. Find Minimum in Rotated Sorted Array Solution 题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成 ...

  4. Find Minimum in Rotated Sorted Array leetcode

    原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...

  5. Search in Rotated Sorted Array leetcode java

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  6. Find Minimum in Rotated Sorted Array——LeetCode

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...

  8. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

随机推荐

  1. grep 相关

    1) -q 参数,本意是 Quiet; do not write anything to standard output.  Exit immediately with zero status if ...

  2. 地址标记,SpringMVC转发与调用相关知识存档

    1.mytest_mavenprj1中,index的 <a href="login/login.html">点击登录</a> 与 <a href=&q ...

  3. nginx apache负载均衡测试

    apache配置 (监听内网ip和端口) Listen 10.163.170.8:8001 Listen 10.163.170.8:8002 Listen 10.163.170.8:8003 < ...

  4. 【转】APNs消息推送完整讲解

    https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificat ...

  5. UVA 11732 strcmp() Anyone? (压缩版字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. poj 3613 经过k条边最短路 floyd+矩阵快速幂

    http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路, ...

  7. NSString几个函数

    第一.判断是否包含前缀于后缀 NSString *str=@"hello andy yang"; if([str hasPrefix:@"hello"]) { ...

  8. Dynamic CRM 2013学习笔记(三十)Linq使用报错 A proxy type with the name account has been defined by another assembly

    在CRM中使用linq时,有时会报这个错误: A proxy type with the name account has been defined by another assembly. Curr ...

  9. C#设计模式(13)——代理模式(Proxy Pattern)

    一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访问到这些对象,如果直接访问对象给系统带来不必要的复杂性,这时候可以在客户端和目标对象之间增加一层中间层,让代 ...

  10. C++ Primer学习笔记一

    /* 题目要求把字符串BRGBBGRRGBBGBBBGRRGBGRG按RGB顺序排列,空间复杂度为O(1) */#include<iostream> using namespace std ...