LeetCode-Find Minimum in Rotated Sorted Array(找出反转数组中的最小值)

题1:

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.

题2:如果允许上题的数组有重复数字呢?

解法:最笨的方法,也是最容易想到的方法就是直接遍历数组,找出最小值,时间复杂度O(n),但是对于本题明显会超时。此时,比较容易想到的就是二分查找,因为其有序。但是两者肯定是有不同点的。

第一题的代码如下:

public int findMin(int[] num) {
        int low = 0;
        int high = num.length-1;
        while (low < high-1) {
        	int mid = low + (high-low)/2;
        	if (num[low] < num[high]) {
        		if (num[mid] < num[low]) {
        			low = mid;
        		} else {
        			high = mid;
        		}
        	} else {
        		if (num[mid] < num[high]) {
        			high = mid;
        		} else {
        			low = mid;
        		}
        	}
        }
        return num[low] > num[high] ? num[high] : num[low];
    }

此题一定要区分,这个序列是递增的还是递减的。很多解题方法都没考虑这个问题。显然有了第一题的思路,第二题也就好解了。

public int findMin(int[] num) {
        int low = 0;
        int high = num.length-1;
        while (low < high-1) {
        	int mid = low + (high-low)/2;
        	if (num[low] < num[high]) {
        		if (num[mid] < num[low]) {
        			low = mid;
        		} else if (num[mid] > num[low]){
        			high = mid;
        		} else {
        			low++;
        		}
        	} else {
        		if (num[mid] < num[high]) {
        			high = mid;
        		} else if (num[mid] > num[high]){
        			low = mid;
        		} else {
        			high--;
        		}
        	}
        }
        return num[low] > num[high] ? num[high] : num[low];
    }
时间: 2024-11-08 20:06:24

LeetCode-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. 这道题是要求找出一个数组中的最小值,但是这个数组是在有序数组的基础上循环右移了K次. 提示可以用二分

【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 -Find Minimum in Rotated Sorted Array II (1)

本人大三狗,大一学物理,大二转专业来了计院.一入计院深似海,从此节操是路人.转眼间一年过去了,基本上课本的知识学的很好,考前突击分数还很光鲜,但是总是觉得空虚.因为在这个讲究技术的年代,没有一点技术压身,是很容易睡不着觉的.近日阅读了不少前人的经验教训,感觉自己的目标很明确,应届入bat,有必要考个研也没问题,保研估计没戏了.在这个讲究实战的年代,我有必要积累一点代码行数了,否则坑定是混不过面试的.而且还自以为是地定制了一批书单,现在看到堆到50cm搞的一堆书,也觉得压力山大.我就是属于这种书看

LeetCode Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 23090 Total Submissions: 73108 My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexi

Leetcode#153Find Minimum in Rotated Sorted Array

Find Minimum in Rotated Sorted Array Total Accepted: 42341 Total Submissions: 127863My Submissions Question Solution 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

Leetcode#154Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 25678 Total Submissions: 80978My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity

Leetcode Find Minimum in Rotated Sorted Array I and 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. You may assume no duplicate exists in the array. 在任何一个sublist中,如果头元素大于尾元素,那么这个minimum一定在这个sublist中间

leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

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 determine if a given target is in the array. 思路:此题在解的时候,才发现Search in Rotated Sorted Array

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1