Skip to content

Commit fe9736c

Browse files
committed
[Function add]
1. Add leetcode solutions with tag binary search.
1 parent d74e12d commit fe9736c

6 files changed

+148
-2
lines changed

leetcode/153. Find Minimum in Rotated Sorted Array.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,22 @@ class Solution {
8585
}
8686
}
8787
```
88+
89+
### Third Time
90+
* Method 1: Binary search
91+
```Java
92+
class Solution {
93+
public int findMin(int[] nums) {
94+
return search(nums, 0, nums.length - 1);
95+
}
96+
private int search(int[] nums, int low, int high){
97+
if(low >= high) return nums[low];
98+
int mid = low + (high - low) / 2;
99+
if(nums[mid] >= nums[low]){
100+
return Math.min(nums[low], search(nums, mid + 1, high));
101+
}else{
102+
return Math.min(nums[mid], search(nums, low, mid - 1));
103+
}
104+
}
105+
}
106+
```

leetcode/154. Find Minimum in Rotated Sorted Array II.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,22 @@ class Solution {
101101
return Math.min(nums[mid], find(nums, low + 1, mid - 1));
102102
}
103103
}
104-
```
104+
```
105+
106+
### Fourth Time
107+
* Method 1: binary search
108+
```Java
109+
class Solution {
110+
public int findMin(int[] nums) {
111+
return findMin(nums, 0, nums.length - 1);
112+
}
113+
private int findMin(int[] nums, int low, int high){
114+
if(low >= high) return nums[low];
115+
int mid = low + (high - low) / 2;
116+
while(low < mid && nums[low] == nums[mid]) low++;
117+
while(high > mid && nums[high] == nums[mid]) high--;
118+
if(nums[mid] >= nums[low]) return Math.min(nums[low], findMin(nums, mid + 1, high));
119+
else return Math.min(nums[mid], findMin(nums, low, mid - 1));
120+
}
121+
}
122+
```

leetcode/162. Find Peak Element.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,22 @@ class Solution {
8383
}
8484
```
8585

86+
### Third Time
87+
* Method 1: Binary Search
88+
```Java
89+
class Solution {
90+
public int findPeakElement(int[] nums) {
91+
if(nums.length == 1) return 0;
92+
int low = 0, high = nums.length - 1;
93+
while(low < high){
94+
int mid = low + (high - low) / 2;
95+
if(nums[mid] < nums[mid + 1]) low = mid + 1;
96+
else high = mid;
97+
}
98+
return low;
99+
}
100+
}
101+
```
102+
86103
### Reference
87104
1. [leetcode 162. Find Peak Element-查找峰元素|二分查找](https://blog.csdn.net/happyaaaaaaaaaaa/article/details/51590056)

leetcode/33. Search in Rotated Sorted Array.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,27 @@ class Solution {
116116
return -1;
117117
}
118118
}
119-
```
119+
```
120+
121+
### Third Time
122+
* Method 1: Binary search: AC 100%
123+
```Java
124+
class Solution {
125+
public int search(int[] nums, int target) {
126+
if(nums == null || nums.length == 0) return -1;
127+
return search(nums, 0, nums.length - 1, target);
128+
}
129+
private int search(int[] nums, int low, int high, int target){
130+
int mid = low + (high - low) / 2;
131+
if(low > high) return -1;
132+
if(nums[mid] == target) return mid;
133+
else if(nums[mid] >= nums[low]){ //left is in order
134+
if(target >= nums[low] && target < nums[mid]) return search(nums, low, mid - 1, target);
135+
else return search(nums, mid + 1, high, target);
136+
}else{ //right is in order
137+
if(target > nums[mid] && target <= nums[high]) return search(nums, mid + 1, high, target);
138+
else return search(nums, low, mid - 1, target);
139+
}
140+
}
141+
}
142+
```

leetcode/81. Search in Rotated Sorted Array II.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,28 @@ class Solution {
7979
}
8080
}
8181
```
82+
83+
### Third Time
84+
* Method 1: binary search, I get the answer by myself
85+
```Java
86+
class Solution {
87+
public boolean search(int[] nums, int target) {
88+
if(nums == null || nums.length == 0) return false;
89+
return search(nums, 0, nums.length - 1, target);
90+
}
91+
private boolean search(int[] nums, int low, int high, int target){
92+
if(low > high) return false;
93+
int mid = low + (high - low) / 2;
94+
if(nums[mid] == target) return true;
95+
while(low < mid && nums[low] == nums[mid]){low++;}
96+
while(high > mid && nums[high] == nums[mid]){high--;}
97+
if(nums[mid] >= nums[low]){ // left side is in order
98+
if(target >= nums[low] && target < nums[mid]) return search(nums, low, mid - 1, target);
99+
else return search(nums, mid + 1, high, target);
100+
}else{ //right side is in order
101+
if(target > nums[mid] && target <= nums[high]) return search(nums, mid + 1, high, target);
102+
else return search(nums, low, mid - 1, target);
103+
}
104+
}
105+
}
106+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## 852. Peak Index in a Mountain Array
2+
3+
### Question:
4+
Let's call an array A a mountain if the following properties hold:
5+
* A.length >= 3
6+
* There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
7+
8+
Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
9+
10+
```
11+
Example 1:
12+
13+
Input: [0,1,0]
14+
Output: 1
15+
16+
Example 2:
17+
18+
Input: [0,2,1,0]
19+
Output: 1
20+
```
21+
22+
Note:
23+
1. 3 <= A.length <= 10000
24+
2. 0 <= A[i] <= 10^6
25+
3. A is a mountain, as defined above.
26+
27+
28+
### Solution:
29+
* Method 1: Binary search
30+
```Java
31+
class Solution {
32+
public int peakIndexInMountainArray(int[] A) {
33+
int low = 0, high = A.length - 1;
34+
while(low < high){
35+
int mid = low + (high - low) / 2;
36+
if(A[mid] < A[mid + 1]) low = mid + 1;
37+
else high = mid;
38+
}
39+
return low;
40+
}
41+
}
42+
```
43+
44+

0 commit comments

Comments
 (0)