Skip to content

Commit 0b65ce5

Browse files
committed
feat: 新增算法和promise文章总结
1 parent 092f814 commit 0b65ce5

File tree

3 files changed

+236
-45
lines changed

3 files changed

+236
-45
lines changed

docs/views/algorithm/2022-04-08.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,170 @@ tags:
1111

1212
之前的[算法打卡](https://zjgyb.github.io/views/algorithm/2021-5-30.html)已经快一年了,由于代码行数较多,因此打开缓慢,故决定再新建一个,用于下一周期的算法打卡
1313

14+
## 【字符串】检查相同字母间的距离
15+
[地址](https://leetcode.cn/problems/check-distances-between-same-letters/)
16+
17+
```ts
18+
function checkDistances(s: string, distance: number[]): boolean {
19+
const map = new Map()
20+
for (let i = 0; i < s.length; i++) {
21+
if (map.has(s[i])) {
22+
const index = s[i].charCodeAt(0) - 'a'.charCodeAt(0)
23+
if (distance[index] !== i - map.get(s[i]) - 1) {
24+
return false
25+
}
26+
} else {
27+
map.set(s[i], i)
28+
}
29+
}
30+
31+
return true
32+
};
33+
```
34+
35+
## 【双指针】最长优雅子数组
36+
[地址](https://leetcode.cn/problems/longest-nice-subarray/)
37+
38+
```ts
39+
function longestNiceSubarray(nums: number[]): number {
40+
const len = nums.length
41+
if (len === 1) {
42+
return 1
43+
}
44+
45+
let prev = 0
46+
let next = 1
47+
let result = 0
48+
49+
while (next < len) {
50+
// 确定新加入的是否与之前的&为0
51+
let i = next - 1
52+
while(i >= prev) {
53+
if ((nums[i] & nums[next]) !== 0) {
54+
prev = i + 1
55+
break
56+
}
57+
i--
58+
}
59+
result = Math.max(result, next - prev + 1)
60+
next++
61+
}
62+
return result
63+
};
64+
```
65+
66+
## 【链表】奇偶链表
67+
[地址](https://leetcode.cn/problems/odd-even-linked-list/)
68+
69+
```ts
70+
// 大致思路
71+
// 比如1->2->3->4->5->6->7
72+
// 第一步2->3互换,然后变成1->3->2->4->5->6->7
73+
// 第二步2->4与5互换,变成1->3->5->2->4->6->7
74+
// 第三步2->4->6与7互换,变成1->3->5->7->2->4->6
75+
function oddEvenList(head: ListNode | null): ListNode | null {
76+
let node = head
77+
let index = 1
78+
79+
while(node) {
80+
let j = index
81+
let next = node
82+
while (j > 0) {
83+
next = next.next
84+
j--
85+
}
86+
87+
if (!next) {
88+
break
89+
}
90+
91+
let afterNext = next.next
92+
if (!afterNext) {
93+
break
94+
}
95+
96+
next.next = next.next.next
97+
afterNext.next = null
98+
next = node.next
99+
node.next = afterNext
100+
node.next.next = next
101+
node = node.next
102+
index++
103+
}
104+
105+
return head
106+
};
107+
```
108+
109+
```ts
110+
// 一个取奇数链表
111+
// 一个取偶数链表
112+
function oddEvenList(head: ListNode | null): ListNode | null {
113+
if (!head) {
114+
return head
115+
}
116+
let evenNode = head
117+
let oddNode = head.next
118+
119+
let evenList = evenNode
120+
let oddList = oddNode
121+
let node = head.next
122+
let index = 0
123+
124+
while(node) {
125+
if (index % 2) {
126+
oddList.next = node.next
127+
oddList = oddList.next
128+
} else {
129+
evenList.next = node.next
130+
if (node.next) {
131+
evenList = evenList.next
132+
}
133+
}
134+
node = node.next
135+
index++
136+
}
137+
evenList.next = oddNode
138+
return evenNode
139+
};
140+
```
141+
142+
## LRU 缓存
143+
[地址](https://leetcode.cn/problems/lru-cache/)
144+
145+
```ts
146+
class LRUCache {
147+
max: number
148+
cache: Map<number, number>
149+
150+
constructor(capacity: number) {
151+
this.max = capacity
152+
this.cache = new Map()
153+
}
154+
155+
get(key: number): number {
156+
if (this.cache.has(key)) {
157+
const val = this.cache.get(key)
158+
this.cache.delete(key)
159+
this.cache.set(key, val)
160+
}
161+
return this.cache.get(key) ?? -1
162+
}
163+
164+
put(key: number, value: number): void {
165+
this.cache.delete(key)
166+
this.cache.set(key, value)
167+
168+
if (this.cache.size > this.max) {
169+
for (const key of this.cache.keys()) {
170+
this.cache.delete(key)
171+
break
172+
}
173+
}
174+
}
175+
}
176+
```
177+
14178
## 【链表】重排链表
15179
[地址](https://leetcode.cn/problems/reorder-list/)
16180

docs/views/js/2022/2022-03-15.md

Lines changed: 0 additions & 44 deletions
This file was deleted.

docs/views/js/2022/2022-08-29.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,77 @@
11
---
2-
title: Promise总结
2+
title: Promise专题
33
date: 2022-08-29
44
categories: JavaScript
55
---
66

7+
## 特点
8+
9+
1. 有三种状态,分别为 `pending``fulfilled``reject`,其他操作无法改变
10+
2. 状态只能从`pending``fulfilled``reject`
11+
12+
## 优、缺点
13+
14+
优点:
15+
1. 以同步的流程表达出来,避免了回调函数的层层嵌套
16+
17+
缺点:
18+
1. 无法取消
19+
2. 内部抛出的错误无法反应到外部
20+
3. 处于`pending`状态无法知道处于哪一个状态
21+
22+
## 注意点
23+
24+
1.`new Promise`中如果没有使用`resolve``reject`,那么promise的状态一直在`pending`
25+
2. `catch`存在跳过的现象
26+
3. 返回一个非Promise的值会被`Promise.resolve`包裹
27+
4. 不能返回自身否则会报错
28+
5. 传入非函数会发生值渗透
29+
30+
```ts
31+
Promise.resolve(1).then(2).then(Promise.resolve(3)).then(console.log) // 1
32+
```
33+
34+
## 手写Promise相关方法
35+
36+
### Promise.all
37+
38+
```ts
39+
if (!Promise.all) {
40+
Promise.all = (arr: unknown[]) => {
41+
return new Promise((resolve, reject) => {
42+
const result: Promise<unknown>[] = []
43+
const getResult = (index: number, value) => {
44+
result[index] = value
45+
46+
if (result.length === arr.length) {
47+
resolve(result)
48+
}
49+
}
50+
for (let i = 0; i < arr.length; i++) {
51+
Promise.resolve(arr[i]).then((value) => {
52+
getResult(i, value)
53+
}, reject)
54+
}
55+
})
56+
}
57+
}
58+
```
59+
60+
### Promise.race
61+
62+
```ts
63+
if (!Promise.race1) {
64+
Promise.race1 = (arr: Promise<unknown>[]) => {
65+
return new Promise((resolve, reject) => {
66+
for (let i = 0; i < arr.length; i++) {
67+
Promise.resolve(arr[i]).then(resolve, reject)
68+
}
69+
})
70+
}
71+
}
72+
```
73+
74+
## 参考文章
75+
76+
- [掘金](https://juejin.cn/post/6844904077537574919)
77+
- [阮一峰](https://es6.ruanyifeng.com/#docs/promise#Promise-%E7%9A%84%E5%90%AB%E4%B9%89)

0 commit comments

Comments
 (0)