[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.
例題給出的是遞增序列經過旋轉後得到的,題目本身意思可能也是遞增旋轉,因為系統給出的解法也是針對遞增的。這裡,用的遞迴來解的話,可以避
免原序列的增減情況。主要思想如下:
1、先判斷原序列是不是遞增或遞減序列,是的話,直接返回第一個或最後一個。否則,進行下一步。
2、每次選擇序列第一個、最後一個和中間的3個數字中最大的和最小的,其中,中間位置的數一定是三個當中最大的或最小的(這是因為,序列是由一個sorted array 旋轉而來的);當最後只剩下2個數字時,就是該書組中最小和最大的那2個,若要獲得最小數字,返回較小的數字即可,餘下的那個是陣列中最大的數字。
程式碼如下:
public int findMin(int[] num) {
if (num.length == 1)
return num[0];
int mid = (num.length - 1) / 2;
if (num[mid] >= num[0] && num[mid] <= num[num.length - 1])// 遞增序列
return num[0];
if (num[mid] <= num[0] && num[mid] >= num[num.length - 1])// 遞減序列
return num[num.length - 1];
return searchMin(num, 0, num.length - 1);
}
public int searchMin(int[] num, int start, int end) {
if (start + 1 == end)
return Math.min(num[start], num[end]);
int mid = (start + end) / 2;
if (num[mid] > Math.max(num[start], num[end])) {
if (num[start] > num[end])
return searchMin(num, mid, end);
else
return searchMin(num, start, mid);
} else {
if (num[start] > num[end])
return searchMin(num, start, mid);
else
return searchMin(num, mid, end);
}
}
這個是不包含重複序列的,重複序列的解法類似。
相關文章
- Find Minimum in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode-Find Minimum in Rotated Sorted Array IILeetCode
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode 153. Find Minimum in Rotated Sorted ArrayLeetCode
- [LeetCode] Search in Rotated Sorted Array 在旋轉有序陣列中搜尋LeetCode陣列
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted Array IILeetCode
- Leetcode-Search in Rotated Sorted ArrayLeetCode
- Search in Rotated Sorted Array leetcode javaLeetCodeJava
- leetcode33_Search in Rotated Sorted ArrayLeetCode
- LeetCode-Search in Rotated Sorted Array IILeetCode
- Search in Rotated Sorted Array II leetcode javaLeetCodeJava
- [CareerCup] 11.3 Search in Rotated Sorted Array 在旋轉有序矩陣中搜尋矩陣
- Java的自動遞增和遞減Java
- php遞增和遞減運算子的介紹PHP
- tkinter中spinbox遞增和遞減控制元件(十)控制元件
- Leetcode 34 Find First and Last Position of Element in Sorted ArrayLeetCodeAST
- LeetCode C++ 33. Search in Rotated Sorted Array【二分】中等LeetCodeC++
- 同步遞增(遞減)的齊次3項恆等式恆等式
- iOS-Swift中的遞增(++)和遞減(--)被取消的原因-官方答覆iOSSwift
- 遞增、遞減運算子位於變數前後的區別變數
- Leetcode Merge Sorted ArrayLeetCode
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode-Merge Sorted ArrayLeetCode
- JavaScript (--) 遞減運算子JavaScript
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode 88. Merge Sorted ArrayLeetCode
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Convert Sorted Array to BSTLeetCode
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Median of Two Sorted Array leetcode javaLeetCodeJava
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava
- [LeetCode] Find First and Last Position of Element in SortedLeetCodeAST
- 【Leetcode】453. Minimum Moves to Equal Array ElementsLeetCode
- Leetcode 453. Minimum Moves to Equal Array ElementsLeetCode