Skip to content

Commit fbaeb3b

Browse files
committed
docs: 新增算法解题
1 parent 0b65ce5 commit fbaeb3b

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

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

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

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

14+
## 【贪心算法】盛最多水的容器
15+
[地址](https://leetcode.cn/problems/container-with-most-water/)
16+
17+
```ts
18+
// 从左到右,从右到左,分别计算
19+
function maxArea(height: number[]): number {
20+
const len = height.length
21+
let result = 0
22+
for (let i = 0; i < len; i++) {
23+
let last = len - 1
24+
while (last > i && height[last] < height[i]) {
25+
last--
26+
}
27+
28+
result = Math.max(result, (last - i) * height[i])
29+
}
30+
31+
for (let i = len - 1; i >= 0; i--) {
32+
let last = 0
33+
while (last < i && height[last] < height[i]) {
34+
last++
35+
}
36+
37+
result = Math.max(result, (i - last) * height[i])
38+
}
39+
40+
return result
41+
};
42+
```
43+
44+
【双指针】:从最左和最右开始,较小的值移动
45+
46+
```ts
47+
function maxArea(height: number[]): number {
48+
let result = 0
49+
let prev = 0
50+
let next = height.length - 1
51+
while (prev < next) {
52+
result = Math.max(result, Math.min(height[prev], height[next]) * (next - prev))
53+
height[prev] > height[next] ? next-- : prev++
54+
}
55+
return result
56+
};
57+
```
58+
59+
60+
## 【字符串】最简分数
61+
[地址](https://leetcode.cn/problems/simplified-fractions/)
62+
```ts
63+
function simplifiedFractions(n: number): string[] {
64+
const result: string[] = []
65+
let prev = 1
66+
67+
// 判断是否存在最小公倍数
68+
const isPair = (prev: number, next: number) => {
69+
const half = Math.min(Math.floor(next / 2), prev)
70+
let start = 2
71+
while(start <= half) {
72+
if (prev % start === 0 && next % start === 0) {
73+
return true
74+
}
75+
start++
76+
}
77+
return false
78+
}
79+
80+
81+
while(prev <= n) {
82+
let next = prev + 1
83+
while(next <= n) {
84+
if (prev === 1 || !isPair(prev, next)) {
85+
result.push(`${prev}/${next}`)
86+
}
87+
next++
88+
}
89+
prev++
90+
}
91+
92+
return result
93+
};
94+
```
95+
96+
```ts
97+
function simplifiedFractions(n: number): string[] {
98+
const result: string[] = []
99+
const set = new Set()
100+
let prev = 1
101+
102+
while(prev <= n) {
103+
let next = prev + 1
104+
while(next <= n) {
105+
if (prev === 1 || !set.has(`${prev},${next}`)) {
106+
result.push(`${prev}/${next}`)
107+
let i = 2
108+
109+
// 存起来
110+
while(next * i <= n) {
111+
set.add(`${prev * i},${next * i}`)
112+
i++
113+
}
114+
}
115+
next++
116+
}
117+
prev++
118+
}
119+
120+
return result
121+
};
122+
```
123+
124+
## 【字符串】 从英文中重建数字
125+
[地址](https://leetcode.cn/problems/reconstruct-original-digits-from-english/)
126+
127+
```ts
128+
// 找到特征字符
129+
function originalDigits(s: string): string {
130+
const obj = {
131+
'zero': 0,
132+
'xis': 6,
133+
'seven': 7,
134+
'wto': 2,
135+
'geiht': 8,
136+
'vfie': 5,
137+
'ufor': 4,
138+
'rthee': 3,
139+
'one': 1,
140+
'inne': 9
141+
}
142+
const words = {}
143+
for (const word of s) {
144+
words[word] = (words[word] || 0) + 1
145+
}
146+
147+
const arr: number[] = Array(10).fill(0);
148+
Object.keys(obj).forEach((key) => {
149+
if (words[key[0]]) {
150+
let num = words[key[0]]
151+
arr[obj[key]] = num
152+
for (const k of key) {
153+
words[k] -= num
154+
}
155+
}
156+
})
157+
158+
return arr.reduce((prev, next, index) => {
159+
if (next !== 0) {
160+
prev += `${index}`.repeat(next)
161+
}
162+
return prev
163+
}, '')
164+
};
165+
```
166+
167+
## 【字符串】字母异位词分组
168+
[地址](https://leetcode.cn/problems/group-anagrams/)
169+
170+
```ts
171+
function groupAnagrams(strs: string[]): string[][] {
172+
const results: string[][] = []
173+
const map = new Map()
174+
175+
for (const str of strs) {
176+
// 切割后排序
177+
const result = str.split('').sort((a, b) => a.codePointAt(0) - b.codePointAt(0)).join('')
178+
179+
if (map.has(result)) {
180+
results[map.get(result)].push(str)
181+
} else {
182+
map.set(result, results.length)
183+
results[results.length] = [str]
184+
}
185+
}
186+
187+
return results
188+
};
189+
```
190+
191+
## 【链表】合并零之间的节点
192+
[地址](https://leetcode.cn/problems/merge-nodes-in-between-zeros/)
193+
194+
```ts
195+
// 思路就是快慢指针
196+
function mergeNodes(head: ListNode | null): ListNode | null {
197+
if (!head) {
198+
return head
199+
}
200+
201+
let fast = head
202+
let slow = head
203+
let val = 0
204+
205+
while (fast?.next) {
206+
val += fast.val
207+
if (fast.next.val !== 0) {
208+
fast = fast.next
209+
continue
210+
}
211+
212+
slow.next = fast.next
213+
fast.next.val = val
214+
val = 0
215+
slow = slow.next
216+
fast = slow.next
217+
}
218+
return head.next
219+
};
220+
```
221+
14222
## 【字符串】检查相同字母间的距离
15223
[地址](https://leetcode.cn/problems/check-distances-between-same-letters/)
16224

0 commit comments

Comments
 (0)