-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.java
More file actions
51 lines (38 loc) · 1.09 KB
/
Copy pathday13.java
File metadata and controls
51 lines (38 loc) · 1.09 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
//ques1.153:Find Minimum in Rotated Sorted Array
//link:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
class Solution {
public int findMin(int[] nums) {
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (nums[mid] > nums[high]) {
low = mid + 1;
} else {
high = mid;
}
}
return nums[low];
}
}
//TC:(LOGN)
//SC:O(1)
//ques2:704:Binary Search
//link:https://leetcode.com/problems/binary-search/description/
class Solution {
public int search(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (target == nums[mid]) {
return mid;
} else if (target > nums[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
}
//TC:(LOGN)
//SC:O(1)