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.

解题思路:

首先假设一个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         }
10         return num[low];
11     }

时间: 2024-12-04 11:30:00

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

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. If found in the array return its index, otherwise return -1. You may assume no du

【leetcode】Find Minimum in Rotated Sorted Array II 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? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m

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

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

Find Minimum in Rotated Sorted Array leetcode

原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! 1 class Solution { 2 public: 3 int findMin(vector<int>& nums) { 4 if (nums.empty()) 5 return -1; 6 int res = find(nums, 0, nums.size()-1);//好神奇,第二个参数无论减或者不减1,都不影响该题的结果 7 if (res == -

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 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 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 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates. 解题思路: 参考Java for LeetCode 081 Search in Rotated Sorted Array II J

Hard??没搞错吧,LeetCode水题一道:Find Minimum in Rotated Sorted Array II

做完Find Minimum in Rotated Sorted Array I觉得很简单,再做2发现也不难,但是LeetCode竟然给了一个hard评级,难道是我编程水平大有长进?哈哈我估计是对复杂度有很高的要求的情况下比较难吧.. Python(偷懒)做法就是一句话:return min(num) Java(基本做法)如下: public class Solution { public int findMin(int[] num) { int len=num.length; if (len=

[LeetCode] 154. 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