【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 may assume no duplicate exists in the array.

题目大意

  假设一个排序的数组以事先未知的某个中枢进行了旋转。

  即,0 1 2 4 5 6 7可能成为4 5 6 7 0 1 2)。

  找到最小的元素。

  你可以假设不存在重复的数组中。

解题思路

  二分搜索法,数组分为两个有序的部分,前一个部分和后一个部分并且前一个排序部分元素都比后一个元素大只要找到后一个元素比前一个大就是要找的元素

代码实现

算法实现类

public class Solution {

    public int findMin(int[] nums) {
        // 参数检验
        if (nums == null || nums.length < 0) {
            throw new IllegalArgumentException();
        }

        return binarySearch(nums, 0, nums.length - 1);
    }

    public int binarySearch(int[] nums, int start, int end) {

        int mid = 0;

        while (start < end) {
            mid = start + ((end - start) >> 1);
            // 后一个数比前个数小就找到了
            if (nums[mid]> nums[mid + 1]) {
                return nums[mid + 1];
            }
            // 说明中间值在第一个有序的数组中
            else if (nums[mid] > nums[start]) {
                start = mid;
            }
            // 说明中间值在第二个有序的数组中
            else {
                end = mid;
            }
        }

        // 说明整个数组是有序的
        return nums[0];
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47828181

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-16 08:55:04

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

【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数

Add Date 2014-10-15 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

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-面试算法经典-Java实现】【155-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)】

[154-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Supp

[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

【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】

[033-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 might become 4 5 6 7 0 1 2). You are given a target value to search.

【LeetCode-面试算法经典-Java实现】【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】

[081-Search in Rotated Sorted Array II(搜索旋转的排序数组)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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

153. Find Minimum in Rotated Sorted Array - LeetCode

Question 153.?Find Minimum in Rotated Sorted Array Solution 题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成[4,5,6,1,2,3],把1求出来 思路:遍历,如果当前元素比前一个元素小就是这个元素了 Java实现: public int findMin(int[] nums) { int ans = nums[0]; for (int i=0; i<nums.length; i++) { int pre

【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】

[026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for anot

Java for LeetCode 153 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. 解题思路: 本题和Java for LeetCode 033 Search in Rotated S