-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinRotatedSortdArray.java
70 lines (46 loc) · 1.22 KB
/
MinRotatedSortdArray.java
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
65
66
67
68
69
70
// Question number 153 in Leetcode
// first solution
class Solution {
public int findMin(int[] nums) {
int firstElement = nums[0];
int i = nums.length-1;
int incidentElm =0;
if(firstElement<nums[i]){
return firstElement;
}
if(i==0){
return firstElement;
}
while(true){
if(nums[i]<firstElement){
incidentElm=nums[i];
i--;
}
else{
break;
}
}
return incidentElm;
}
}
Binary Search Solution
class Solution {
public int findMin(int[] nums) {
int l=0,r=nums.length-1;
int mid=0;
while(l<r){
//array is sorted
if(nums[l]<nums[r]){
return nums[l];
}
mid=(r+l)/2;
if(nums[mid]<nums[l]){
r=mid;
}
else{
l=mid+1;
}
}
return nums[r];
}
}