Skip to content

Commit 9c6fdc7

Browse files
Merge pull request #333 from jsrdxzw/master
增加二分法的变种应用
2 parents 1b06375 + 2612b92 commit 9c6fdc7

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* 二分查找法在实际的场合使用不多
3+
* 我们更多的是看到一些查找法的变种
4+
*/
5+
class BinaryFind {
6+
/**
7+
* 在数组中查找第一个等于给定值的位置
8+
* @param array 给定的要查找的范围数组
9+
* @param target 要查找的目标值
10+
* @return 目标在数组中的位置
11+
*/
12+
static findFirstElement(array: number[], target: number): number {
13+
const length = array ? array.length : null
14+
if (!length || length === 0) return -1
15+
let low = 0
16+
let high = length - 1
17+
while (low <= high) {
18+
const mid = low + ((high - low) >> 2)
19+
if (array[mid] > target) {
20+
high = mid - 1
21+
} else if (array[mid] < target) {
22+
low = mid + 1
23+
} else {
24+
if (mid === 0 || array[mid - 1] !== target) {
25+
return mid
26+
} else {
27+
high = mid - 1
28+
}
29+
}
30+
}
31+
return -1
32+
}
33+
34+
/**
35+
* 在数组中查找最后一个等于给定值的位置
36+
* @param array 给定的要查找的范围数组
37+
* @param target 要查找的目标值
38+
*/
39+
static findLastElement(array: number[], target: number): number {
40+
const length = array ? array.length : null
41+
if (!length || length === 0) return -1
42+
let low = 0
43+
let high = length - 1
44+
while (low <= high) {
45+
const mid = low + ((high - low) >> 2)
46+
if (array[mid] > target) {
47+
high = mid - 1
48+
} else if (array[mid] < target) {
49+
low = mid + 1
50+
// 这里已经是找到相等的元素了
51+
} else {
52+
if (mid === length - 1 || array[mid + 1] !== target) {
53+
return mid
54+
} else {
55+
low = mid + 1
56+
}
57+
}
58+
}
59+
return -1
60+
}
61+
62+
/**
63+
* 在数组中查找第一个大于等于给定值的位置
64+
* @param array 给定的要查找的范围数组
65+
* @param target 要查找的目标值
66+
*/
67+
static findFirstElementGreaterThanTarget(array: number[], target: number): number {
68+
const length = array ? array.length : null
69+
if (!length || length === 0) return -1
70+
let low = 0
71+
let high = length - 1
72+
while (low <= high) {
73+
const mid = low + ((high - low) >> 2)
74+
if (array[mid] < target) {
75+
low = mid + 1
76+
} else {
77+
if (mid === 0 || array[mid - 1] < target) {
78+
return mid
79+
} else {
80+
high = mid - 1
81+
}
82+
}
83+
}
84+
return -1
85+
}
86+
87+
/**
88+
* 在数组中查找最后一个小于等于给定值的位置
89+
* @param array 给定的要查找的范围数组
90+
* @param target 要查找的目标值
91+
*/
92+
static findLastElementLessThanTarget(array: number[], target: number): number {
93+
const length = array ? array.length : null
94+
if (!length || length === 0) return -1
95+
let low = 0
96+
let high = length - 1
97+
while (low <= high) {
98+
const mid = low + ((high - low) >> 2)
99+
if (array[mid] > target) {
100+
high = mid - 1
101+
} else {
102+
if (mid === length - 1 || array[mid + 1] > target) {
103+
return mid
104+
} else {
105+
low = mid + 1
106+
}
107+
}
108+
}
109+
return -1
110+
}
111+
}
112+
113+
const binaryFindTest = [1, 3, 4, 4, 5, 6, 8, 8, 8, 11, 18]
114+
const target = BinaryFind.findLastElementLessThanTarget(binaryFindTest, -1)
115+
console.log(target)

0 commit comments

Comments
 (0)