O(n)的算法就不说了,这题主要考查的是 O(logn)的算法。

有序数组easy想到使用二分查找解决。这题就是在二分基础上做一些调整。数组仅仅有一次翻转,能够知道原有序递增数组被分成两部分,这俩部分都是有序递增的(这题仅仅须要考虑有序数组的递增情况)。

假如翻转后的数组以第 x 个结点分为两部分 A[0..x] 和 A[x+1..n]。则 A[0..x] 这一段是有序递增的, A[x+1..n] 这一段也是有序递增的。

而且由于原数组是有序递增的,A[0..x] 中全部数都会大于 A[x+1..n] 中的不论什么数。所以我们事实上就是须要找到结点 A[x+1]。这个结点的值就是最小值。

考虑数组 A[i..j],中间结点 m (m = (i + j ) / 2)。

A[i] < A[j]:数组是递增的,说明已经找到 x 结点,而且 x 等于 i。

A[i] >= A[j]:数组不是递增的,说明 x 结点还没有找到,这时对照中间结点 A[m]

A[m] > A[i]: 则数组中 A[i..m] 这一段是有序递增的,翻转结点 x 定不会在这一段中。这时我们仅仅须要考虑 A[m+1..j] 这一段。

A[m] < A[i]:说明翻转结点 x 在 A[i..m]中。

另外特别考虑仅仅有一个元素的情况。

public class Solution {
public int findMin(int[] num) { int left = 0;
int right = num.length - 1; while(left < right)
{
if(num[left] < num[right]) {
return num[left];
} int mid = left + (right-left)/2;
if(num[left] <= num[mid]) {
left = mid + 1;
} else {
right = mid;
}
}
return num[left];
}
}

http://orzorz.me/learn/lesson.htm?lessonId=106

递归

Thoughts:

  1. If the array just has one element, then return the element.
  2. If the array has two elements, then return the smaller one.
  3. If the left most element is smaller than the right most element, then we can know the array is sorted like never be rotated. Just return the left one.
  4. By the method of Binary Search, we get the middle element of array, a[mid]. If a[mid] > a[left], then the left half of array is sorted. we then search the right half, including a[mid]. Otherwise we search the left half, including a[mid].

public class Solution {
public int findMin(int[] A) {
return helper(A, 0, A.length-1);
} public static int helper(int[] a, int left, int right){
//one element
if(left == right){
return a[left];
} //two elements
if(left == right-1){
return a[left]<a[right]? a[left]: a[right];
} //the array is ordered
if(a[left] < a[right]){
return a[left];
} int mid = (left+right)/2; if(a[mid] >= a[left]){
return helper(a, mid, right);
}else{
return helper(a, left, mid);
} }
}

https://chesterli0130.wordpress.com/2012/10/20/finding-the-minimum-in-a-sorted-rotated-array/

Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode的更多相关文章

  1. Find Minimum in Rotated Sorted Array(旋转数组的最小数字)

    题目描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., might become ...

  2. 153 Find Minimum in Rotated Sorted Array 旋转数组的最小值

    假设一个按照升序排列的有序数组从某未知的位置旋转.(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2).找到其中最小的元素.你可以假设数组中不存在重复的元素.详见:https:/ ...

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

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

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

  5. LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)

    题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

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

  7. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

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

  8. [LeetCode] Find Minimum 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 ...

  9. [Swift]LeetCode154. 寻找旋转排序数组中的最小值 II | Find Minimum in Rotated Sorted Array II

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

随机推荐

  1. java内存模型(线程共享部分)

    1.元空间(MetaSpace)与永久代(PermGen)的区别? ----> 1.1 元空间使用的是本机内存(这样的好处是,可以使用的内存空间变大了,没有OutOfMemoryError:Pe ...

  2. PyTorch的十七个损失函数

    本文截取自<PyTorch 模型训练实用教程>,获取全文pdf请点击: tensor-yu/PyTorch_Tutorial​github.com 版权声明:本文为博主原创文章,转载请附上 ...

  3. 如何使用postman访问网站

    1.输入Request URL2.选择Request Method3.输入需要的Request Headers注意:一般token会在Headers中

  4. [LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays

    https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离 ...

  5. 算法导论 第八章 线性时间排序(python)

    比较排序:各元素的次序依赖于它们之间的比较{插入排序O(n**2) 归并排序O(nlgn) 堆排序O(nlgn)快速排序O(n**2)平均O(nlgn)} 本章主要介绍几个线性时间排序:(运算排序非比 ...

  6. DEV Express中NavBarCointrol的使用

    尚未对内容进行删减,内容有偏差和冗余,谨慎阅读. 发现在后面,写在前面: 13,之前在Default模式下,之所以很多Appearance属性都起不到作用,是因为Control的LookAndFeel ...

  7. [BZOJ 3823]定情信物

    题面 定情信物 题解 这题主要考高中物理和数学. 首先定义 \(f[i][j]\) 表示 \(i\) 维超立方体中第 \(j\) 维元素的数量,根据实际意义,我们可以推出递推式: \(f[i][j]= ...

  8. Python+selenium鼠标、键盘事件

    鼠标操作 现在的Web产品提供了丰富的鼠标交互方式,例如鼠标右击.双击.悬停.甚至是鼠标拖动等功能,在Webdriver中,将这些关于鼠标操作的方法封装在ActionChains类提供. 1.鼠标右击 ...

  9. Tao Tao要吃鸡(01背包)

    题目描述 Taotao的电脑带不动绝地求生,所以taotao只能去玩pc版的荒野行动了,和绝地求生一样,游戏人物本身可以携带一定重量m的物品,装备背包之后可以多携带h(h为0代表没有装备背包)重量的东 ...

  10. NYOJ301-递推求值

    递推求值 nyoj上矩阵专题里的10道题水了AC率最高的5道,惭愧,还不是完全自己写的,用了几乎两周的时间.模板题我是有自信写出来的,但对于高级一点的矩阵构造,我还是菜的抠脚. 这题感谢MQL大哥和她 ...