-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMinimumInRotatedSortedArray.java
More file actions
64 lines (58 loc) · 1.63 KB
/
FindMinimumInRotatedSortedArray.java
File metadata and controls
64 lines (58 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* 题目:给定一个数组,该数组按照升序排列,且无重复元素。在某个位置旋转了,求最小元素。
* 例如:
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
* 返回0
* 解题思路:
* 二分查找。
* 首先判断该数组是否旋转了:比较最后一个元素与第一个元素的大小,若最后的元素大于第一个元素,说明没有旋转,直接返回最小的。
* 否则:判断中间位置的数是否大于第一个数,如果是,则在右边寻找;否则,在右边寻找。
*
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FindMinimumInRotatedSortedArray {
public static void main(String[] args) throws IOException {
System.out.println("请输入数组:");
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(isr);
String s=buf.readLine();
String[] st=s.split(" ");
int nums[]=new int[st.length];
for(int i=0;i<st.length;i++)
nums[i]=Integer.parseInt(st[i]);
Solution75 sl=new Solution75();
int result=sl.findMin(nums);
System.out.println("结果是:"+result);
}
}
class Solution75
{
public int findMin(int[] nums)
{
if(nums==null || nums.length==0) return Integer.MAX_VALUE;
int Length=nums.length;
int left=0;
int right=Length-1;
//首先判断是否旋转
if(nums[left]<=nums[right])
{
return nums[left];
}else
{
while(left!=right-1)
{
int mid=(left+right)/2;
if(nums[mid]>nums[left])
{
left=mid;
}else
{
right=mid;
}
}
return Math.min(nums[left], nums[right]);
}
}
}