Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0c29b8

Browse files
committed
增加二分查找
1 parent 652535c commit b0c29b8

File tree

2 files changed

+64
-27
lines changed

2 files changed

+64
-27
lines changed

typescript/src/nowcoder/examTop101.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,37 @@ function BM1() {
2929
console.log(ReverseList(new ListNode(1)))
3030
}
3131
// BM1()
32+
33+
function BM17() {
34+
// 二分查找
35+
const target = 13
36+
const arr = [-1, 0, 3, 4, 6, 10, 13, 14]
37+
38+
const search = function (nums, target) {
39+
// 在区间[left,right]中查找元素,左闭右闭
40+
let left = 0
41+
let right = nums.length - 1
42+
while (left <= right) {
43+
// 计算中间点
44+
let mid = parseInt(left + (right - left) / 2)
45+
if (target == nums[mid]) {
46+
return mid
47+
// 如果target < nums[mid],表示目标值可能在左半边
48+
} else if (target < nums[mid]) {
49+
right = mid - 1
50+
// 如果target > nums[mid],表示目标值可能在右半边
51+
} else if (target > nums[mid]) {
52+
left = mid + 1
53+
}
54+
}
55+
// 未找到返回-1
56+
return -1
57+
}
58+
59+
console.log(search(arr, target))
60+
}
61+
BM17()
62+
63+
function BM23() {
64+
console.log(1)
65+
}

typescript/src/nowcoder/microTask.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
11
// 面试题目一
22

3-
async function async1() {
4-
console.log('async1 start') // 2
5-
await async2()
6-
console.log('async1 end') // 7
7-
}
3+
function events() {
4+
async function async1() {
5+
console.log('async1 start') // 2
6+
await async2()
7+
console.log('async1 end') // 7
8+
}
89

9-
async function async2() {
10-
console.log('async2') // 3
11-
}
10+
async function async2() {
11+
console.log('async2') // 3
12+
}
1213

13-
console.log('script start') // 1
14+
console.log('script start') // 1
1415

15-
setTimeout(function () {
16-
console.log('setTimeout0')
17-
}, 0) // 12
16+
setTimeout(function () {
17+
console.log('setTimeout0')
18+
}, 0) // 12
1819

19-
setTimeout(function () {
20-
console.log('setTimeout2')
21-
}, 300) // 13
20+
setTimeout(function () {
21+
console.log('setTimeout2')
22+
}, 300) // 13
2223

23-
setImmediate(() => console.log('setImmediate')) // 11
24+
setImmediate(() => console.log('setImmediate')) // 11
2425

25-
process.nextTick(() => console.log('nextTick1')) // 9
26+
process.nextTick(() => console.log('nextTick1')) // 9
2627

27-
async1() // 2
28+
async1() // 2
2829

29-
new Promise(function (resolve) {
30-
console.log('promise1') // 4
31-
resolve()
32-
console.log('promise2') // 5
33-
}).then(function () {
34-
console.log('promise3') // 8
35-
})
30+
new Promise(function (resolve) {
31+
console.log('promise1') // 4
32+
resolve()
33+
console.log('promise2') // 5
34+
}).then(function () {
35+
console.log('promise3') // 8
36+
})
3637

37-
process.nextTick(() => console.log('nextTick2')) // 10
38+
process.nextTick(() => console.log('nextTick2')) // 10
3839

39-
console.log('script end') // 6
40+
console.log('script end') // 6
41+
}
42+
// events()
4043

4144
/**
4245
* script start

0 commit comments

Comments
 (0)