Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode

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

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

假如翻转后的数组以第 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/

时间: 2024-10-05 04:24:24

Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @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 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 这道题<剑指offer>上有原题,直接上代码 solution: int findMi

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

假设一个按照升序排列的有序数组从某未知的位置旋转.(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2).找到其中最小的元素.你可以假设数组中不存在重复的元素.详见:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ class Solution { public: int findMin(vector<int>& nums) { int low=0; int

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

【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 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 d

Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode

递归 public class Solution { public int findMin(int[] num) { return helper(num, 0, num.length-1); } //with duplicate public static int helper(int[] a, int left, int right){ //one element if(left == right){ return a[left]; } //two elements if(left == ri

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

题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序没乱,直接返回A[1],如果乱了,二分查找还是可以的,O(1)可能就不行了. 二分要点:mid有可能就是所要找的最小元素,所以不能轻易写出l=mid+1这样的语句,可能最小值就被忽略过了,因为我们无法直接判断A[mid]是否就是最小值.所以尽量应该是l=mid这样写,但是要防止死循环. 具体来说,可

[LeetCode]111. Find Minimum in Rotated Sorted Array II旋转数组最小值II

Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might be

Find Minimum in Rotated Sorted Array 2 寻找旋转有序数组的最小值之二

Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might be

【LeetCode-面试算法经典-Java实现】【153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)】

[153-Find Minimum 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 might become 4 5 6 7 0 1 2). Find the minimum element. You m