Skip to content

Commit dafa769

Browse files
committedJun 23, 2021
feat: add binary search examples
1 parent b5e7929 commit dafa769

File tree

14 files changed

+175
-149
lines changed

14 files changed

+175
-149
lines changed
 

‎basic/searching/BinarySearch/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ int binarySearch2(int left, int right) {
3131

3232
## 例题
3333

34-
- [在排序数组中查找元素的第一个和最后一个位置](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README.md)
34+
- [在排序数组中查找元素的第一个和最后一个位置](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README.md)
35+
- [寻找峰值](/solution/0100-0199/0162.Find%20Peak%20Element/README.md)
36+
- [第一个错误的版本](/solution/0200-0299/0278.First%20Bad%20Version/README.md)
37+
- [不动点](/solution/1000-1099/1064.Fixed%20Point/README.md)

‎basic/searching/BinarySearch/README_EN.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ int binarySearch2(int left, int right) {
2929

3030
## Examples
3131

32-
- [Find First and Last Position of Element in Sorted Array](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README_EN.md)
32+
- [Find First and Last Position of Element in Sorted Array](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README_EN.md)
33+
- [Find Peak Element](/solution/0100-0199/0162.Find%20Peak%20Element/README_EN.md)
34+
- [First Bad Version](/solution/0200-0299/0278.First%20Bad%20Version/README_EN.md)
35+
- [Fixed Point](/solution/1000-1099/1064.Fixed%20Point/README_EN.md)

‎index.html

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-javascript.min.js"></script>
106106
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-typescript.min.js"></script>
107107
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-python.min.js"></script>
108+
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-swift.min.js"></script>
108109
<script src="//cdn.jsdelivr.net/npm/docsify-copy-code@2.1.1/dist/docsify-copy-code.min.js"></script>
109110
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/search.min.js"></script>
110111
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/emoji.min.js"></script>

‎solution/0000-0099/0001.Two Sum/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ var twoSum = function (nums, target) {
9393
return [];
9494
};
9595
```
96+
9697
### **Swift**
9798

9899
```swift

‎solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class Solution {
9494
if (nums.length == 0) {
9595
return new int[]{-1, -1};
9696
}
97+
// find first position
9798
int left = 0, right = nums.length - 1;
9899
while (left < right) {
99100
int mid = (left + right) >>> 1;
@@ -107,6 +108,8 @@ class Solution {
107108
return new int[]{-1, -1};
108109
}
109110
int l = left;
111+
112+
// find last position
110113
right = nums.length - 1;
111114
while (left < right) {
112115
int mid = (left + right + 1) >>> 1;

‎solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Solution {
7272
if (nums.length == 0) {
7373
return new int[]{-1, -1};
7474
}
75+
// find first position
7576
int left = 0, right = nums.length - 1;
7677
while (left < right) {
7778
int mid = (left + right) >>> 1;
@@ -85,6 +86,8 @@ class Solution {
8586
return new int[]{-1, -1};
8687
}
8788
int l = left;
89+
90+
// find last position
8891
right = nums.length - 1;
8992
while (left < right) {
9093
int mid = (left + right + 1) >>> 1;

‎solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/Solution.java

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ public int[] searchRange(int[] nums, int target) {
33
if (nums.length == 0) {
44
return new int[]{-1, -1};
55
}
6+
// find first position
67
int left = 0, right = nums.length - 1;
78
while (left < right) {
89
int mid = (left + right) >>> 1;
@@ -16,6 +17,8 @@ public int[] searchRange(int[] nums, int target) {
1617
return new int[]{-1, -1};
1718
}
1819
int l = left;
20+
21+
// find last position
1922
right = nums.length - 1;
2023
while (left < right) {
2124
int mid = (left + right + 1) >>> 1;

‎solution/0200-0299/0278.First Bad Version/README.md

+52-49
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ class Solution:
4747
:type n: int
4848
:rtype: int
4949
"""
50-
low, high = 1, n
51-
while low < high:
52-
mid = (low + high) >> 1
50+
left, right = 1, n
51+
while left < right:
52+
mid = (left + right) >> 1
5353
if isBadVersion(mid):
54-
high = mid
54+
right = mid
5555
else:
56-
low = mid + 1
57-
return low
56+
left = mid + 1
57+
return left
5858
```
5959

6060
### **Java**
@@ -67,13 +67,16 @@ class Solution:
6767

6868
public class Solution extends VersionControl {
6969
public int firstBadVersion(int n) {
70-
int low = 1, high = n;
71-
while (low < high) {
72-
int mid = (low + high) >>> 1;
73-
if (isBadVersion(mid)) high = mid;
74-
else low = mid + 1;
70+
int left = 1, right = n;
71+
while (left < right) {
72+
int mid = (left + right) >>> 1;
73+
if (isBadVersion(mid)) {
74+
right = mid;
75+
} else {
76+
left = mid + 1;
77+
}
7578
}
76-
return low;
79+
return left;
7780
}
7881
}
7982
```
@@ -87,16 +90,16 @@ public class Solution extends VersionControl {
8790
class Solution {
8891
public:
8992
int firstBadVersion(int n) {
90-
int low = 1, high = n;
91-
while (low < high) {
92-
int mid = low + (high - low) / 2;
93+
int left = 1, right = n;
94+
while (left < right) {
95+
int mid = left + ((right - left) >> 1);
9396
if (isBadVersion(mid)) {
94-
high = mid;
97+
right = mid;
9598
} else {
96-
low = mid + 1;
99+
left = mid + 1;
97100
}
98101
}
99-
return low;
102+
return left;
100103
}
101104
};
102105
```
@@ -106,7 +109,7 @@ public:
106109
```js
107110
/**
108111
* Definition for isBadVersion()
109-
*
112+
*
110113
* @param {integer} version number
111114
* @return {boolean} whether the version is bad
112115
* isBadVersion = function(version) {
@@ -118,49 +121,49 @@ public:
118121
* @param {function} isBadVersion()
119122
* @return {function}
120123
*/
121-
var solution = function (isBadVersion) {
122-
/**
123-
* @param {integer} n Total versions
124-
* @return {integer} The first bad version
125-
*/
126-
return function (n) {
127-
let low = 1,
128-
high = n;
129-
while (low < high) {
130-
const mid = (low + high) >>> 1;
131-
if (isBadVersion(mid)) {
132-
high = mid;
133-
} else {
134-
low = mid + 1;
135-
}
136-
}
137-
return low;
138-
};
124+
var solution = function(isBadVersion) {
125+
/**
126+
* @param {integer} n Total versions
127+
* @return {integer} The first bad version
128+
*/
129+
return function(n) {
130+
let left = 1;
131+
let right = n;
132+
while (left < right) {
133+
const mid = (left + right) >>> 1;
134+
if (isBadVersion(mid)) {
135+
right = mid;
136+
} else {
137+
left = mid + 1;
138+
}
139+
}
140+
return left;
141+
};
139142
};
140143
```
141144

142145
### **Go**
143146

144147
```go
145-
/**
148+
/**
146149
* Forward declaration of isBadVersion API.
147150
* @param version your guess about first bad version
148-
* @return true if current version is bad
151+
* @return true if current version is bad
149152
* false if current version is good
150153
* func isBadVersion(version int) bool;
151154
*/
152155

153156
func firstBadVersion(n int) int {
154-
low, high := 1, n
155-
for low < high {
156-
mid := (low + high) >> 1
157-
if isBadVersion(mid) {
158-
high = mid
159-
} else {
160-
low = mid + 1
161-
}
162-
}
163-
return low
157+
left, right := 1, n
158+
for left < right {
159+
mid := (left + right) >> 1
160+
if isBadVersion(mid) {
161+
right = mid
162+
} else {
163+
left = mid + 1
164+
}
165+
}
166+
return left
164167
}
165168
```
166169

0 commit comments

Comments
 (0)
Please sign in to comment.