Skip to content

Commit 222b616

Browse files
committed
🚧更新文章
1 parent d80804a commit 222b616

File tree

2 files changed

+620
-0
lines changed

2 files changed

+620
-0
lines changed

docs/interview/Algorithm/index.md

Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
---
2+
title: 前端面试 数据结构与算法
3+
tags: front interview
4+
theme: solarized-dark
5+
---
6+
7+
# 前端面试 数据结构与算法
8+
9+
## 1. 常见的数据结构有哪些?
10+
11+
### 1.1 数组(Array)
12+
13+
- 一组相同类型的数据的集合,内存中连续存储。
14+
- 支持快速访问任意元素,但插入和删除效率较低。
15+
16+
#### 示例代码:
17+
18+
```typescript
19+
// 数组初始化和基本操作
20+
let arr: number[] = [1, 2, 3, 4, 5];
21+
22+
// 访问元素
23+
console.log(arr[2]); // 输出 3
24+
25+
// 插入元素
26+
arr.push(6); // 添加到末尾
27+
arr.unshift(0); // 添加到开头
28+
29+
// 删除元素
30+
arr.pop(); // 删除末尾
31+
arr.shift(); // 删除开头
32+
33+
console.log(arr); // 输出 [0, 1, 2, 3, 4, 5]
34+
```
35+
36+
### 1.2 链表(Linked List)
37+
38+
- 由节点构成,每个节点包含数据和指向下一个节点的指针。
39+
- 分为单链表、双链表和循环链表,适合频繁插入和删除操作,但随机访问效率低。
40+
41+
#### 示例代码:
42+
43+
```typescript
44+
// 定义链表节点
45+
class ListNode {
46+
value: number;
47+
next: ListNode | null = null;
48+
49+
constructor(value: number) {
50+
this.value = value;
51+
}
52+
}
53+
54+
// 定义链表
55+
class LinkedList {
56+
head: ListNode | null = null;
57+
58+
// 插入节点到头部
59+
insertAtHead(value: number): void {
60+
const newNode = new ListNode(value);
61+
newNode.next = this.head;
62+
this.head = newNode;
63+
}
64+
65+
// 打印链表
66+
printList(): void {
67+
let current = this.head;
68+
while (current) {
69+
console.log(current.value);
70+
current = current.next;
71+
}
72+
}
73+
}
74+
75+
const list = new LinkedList();
76+
list.insertAtHead(3);
77+
list.insertAtHead(2);
78+
list.insertAtHead(1);
79+
list.printList(); // 输出 1, 2, 3
80+
```
81+
82+
### 1.3 栈(Stack)
83+
84+
- 先进后出的线性结构,只允许在一端进行插入和删除操作。
85+
- 常用于递归、表达式求值等场景。
86+
87+
#### 示例代码:
88+
89+
```typescript
90+
class Stack {
91+
items: number[] = [];
92+
93+
// 入栈
94+
push(item: number): void {
95+
this.items.push(item);
96+
}
97+
98+
// 出栈
99+
pop(): number | undefined {
100+
return this.items.pop();
101+
}
102+
103+
// 获取栈顶元素
104+
peek(): number | undefined {
105+
return this.items[this.items.length - 1];
106+
}
107+
}
108+
109+
const stack = new Stack();
110+
stack.push(1);
111+
stack.push(2);
112+
console.log(stack.pop()); // 输出 2
113+
console.log(stack.peek()); // 输出 1
114+
```
115+
116+
### 1.4 队列(Queue)
117+
118+
- 先进先出的线性结构,只允许在一端插入,在另一端删除。
119+
- 常用于任务调度、消息队列等。
120+
121+
#### 示例代码:
122+
123+
```typescript
124+
class Queue {
125+
items: number[] = [];
126+
127+
// 入队
128+
enqueue(item: number): void {
129+
this.items.push(item);
130+
}
131+
132+
// 出队
133+
dequeue(): number | undefined {
134+
return this.items.shift();
135+
}
136+
}
137+
138+
const queue = new Queue();
139+
queue.enqueue(1);
140+
queue.enqueue(2);
141+
console.log(queue.dequeue()); // 输出 1
142+
console.log(queue.dequeue()); // 输出 2
143+
```
144+
145+
### 1.5 哈希表(Hash Table)
146+
147+
- 通过哈希函数将数据映射到数组的特定位置。
148+
- 支持高效的插入、删除和查找操作。哈希冲突的解决通常有开放寻址法和链地址法等。
149+
150+
#### 示例代码:
151+
152+
```typescript
153+
class HashTable {
154+
table: { [key: string]: any } = {};
155+
156+
// 添加键值对
157+
set(key: string, value: any): void {
158+
this.table[key] = value;
159+
}
160+
161+
// 获取值
162+
get(key: string): any {
163+
return this.table[key];
164+
}
165+
166+
// 删除键值对
167+
delete(key: string): void {
168+
delete this.table[key];
169+
}
170+
}
171+
172+
const hashTable = new HashTable();
173+
hashTable.set("name", "Alice");
174+
console.log(hashTable.get("name")); // 输出 "Alice"
175+
hashTable.delete("name");
176+
```
177+
178+
### 1.6 树(Tree)
179+
180+
- 一种层次结构的数据结构,由节点和边组成。
181+
- 常见的有二叉树、二叉搜索树、AVL 树、红黑树、B 树等,广泛用于数据库和文件系统。
182+
183+
#### 示例代码:
184+
185+
```typescript
186+
class TreeNode {
187+
value: number;
188+
left: TreeNode | null = null;
189+
right: TreeNode | null = null;
190+
191+
constructor(value: number) {
192+
this.value = value;
193+
}
194+
}
195+
196+
class BinarySearchTree {
197+
root: TreeNode | null = null;
198+
199+
// 插入节点
200+
insert(value: number): void {
201+
const newNode = new TreeNode(value);
202+
if (!this.root) {
203+
this.root = newNode;
204+
} else {
205+
this.insertNode(this.root, newNode);
206+
}
207+
}
208+
209+
private insertNode(node: TreeNode, newNode: TreeNode): void {
210+
if (newNode.value < node.value) {
211+
if (!node.left) {
212+
node.left = newNode;
213+
} else {
214+
this.insertNode(node.left, newNode);
215+
}
216+
} else {
217+
if (!node.right) {
218+
node.right = newNode;
219+
} else {
220+
this.insertNode(node.right, newNode);
221+
}
222+
}
223+
}
224+
}
225+
226+
const bst = new BinarySearchTree();
227+
bst.insert(5);
228+
bst.insert(3);
229+
bst.insert(8);
230+
```
231+
232+
### 1.7 堆(Heap)
233+
234+
- 一种特殊的完全二叉树,分为最大堆和最小堆。
235+
- 主要用于实现优先队列和排序算法(如堆排序)。
236+
237+
#### 示例代码:
238+
239+
```typescript
240+
class MinHeap {
241+
heap: number[] = [];
242+
243+
insert(value: number): void {
244+
this.heap.push(value);
245+
this.bubbleUp(this.heap.length - 1);
246+
}
247+
248+
private bubbleUp(index: number): void {
249+
while (index > 0) {
250+
const parentIndex = Math.floor((index - 1) / 2);
251+
if (this.heap[index] >= this.heap[parentIndex]) break;
252+
[this.heap[index], this.heap[parentIndex]] = [
253+
this.heap[parentIndex],
254+
this.heap[index],
255+
];
256+
index = parentIndex;
257+
}
258+
}
259+
}
260+
261+
const minHeap = new MinHeap();
262+
minHeap.insert(3);
263+
minHeap.insert(1);
264+
minHeap.insert(2);
265+
console.log(minHeap.heap); // 输出 [1, 3, 2]
266+
```
267+
268+
### 1.8 图(Graph)
269+
270+
- 由顶点和边构成,分为有向图和无向图。
271+
- 常用于描述网络结构,如社交网络、地图导航等。常见的图算法有深度优先搜索、广度优先搜索、最短路径算法等。
272+
273+
#### 示例代码:
274+
275+
```typescript
276+
class Graph {
277+
adjacencyList: Map<number, number[]> = new Map();
278+
279+
addVertex(vertex: number): void {
280+
if (!this.adjacencyList.has(vertex)) {
281+
this.adjacencyList.set(vertex, []);
282+
}
283+
}
284+
285+
addEdge(vertex1: number, vertex2: number): void {
286+
this.adjacencyList.get(vertex1)?.push(vertex2);
287+
this.adjacencyList.get(vertex2)?.push(vertex1);
288+
}
289+
}
290+
291+
const graph = new Graph();
292+
graph.addVertex(1);
293+
graph.addVertex(2);
294+
graph.addEdge(1, 2);
295+
console.log(graph.adjacencyList); // 输出 Map { 1 => [2], 2 => [1] }
296+
```
297+
298+
### 1.9 字典树(Trie)
299+
300+
#### 示例代码:
301+
302+
```typescript
303+
class TrieNode {
304+
children: Map<string, TrieNode> = new Map();
305+
isEndOfWord: boolean = false;
306+
}
307+
308+
class Trie {
309+
root: TrieNode = new TrieNode();
310+
311+
insert(word: string): void {
312+
let current = this.root;
313+
for (const char of word) {
314+
if (!current.children.has(char)) {
315+
current.children.set(char, new TrieNode());
316+
}
317+
current = current.children.get(char)!;
318+
}
319+
current.isEndOfWord = true;
320+
}
321+
}
322+
323+
const trie = new Trie();
324+
trie.insert("apple");
325+
```
326+
327+
- 一种树形结构,主要用于字符串存储与查找。
328+
- 常见于实现自动补全和拼写检查等。
329+
330+
### 1.10 并查集(Union-Find)
331+
332+
#### 示例代码:
333+
334+
```typescript
335+
class UnionFind {
336+
parent: number[];
337+
338+
constructor(size: number) {
339+
this.parent = Array.from({ length: size }, (_, i) => i);
340+
}
341+
342+
find(x: number): number {
343+
if (this.parent[x] === x) return x;
344+
return (this.parent[x] = this.find(this.parent[x])); // 路径压缩
345+
}
346+
347+
union(x: number, y: number): void {
348+
const rootX = this.find(x);
349+
const rootY = this.find(y);
350+
if (rootX !== rootY) {
351+
this.parent[rootX] = rootY;
352+
}
353+
}
354+
}
355+
356+
const uf = new UnionFind(5);
357+
uf.union(0, 1);
358+
console.log(uf.find(1)); // 输出 1
359+
```
360+
361+
- 用于处理不相交集合的数据结构。
362+
- 常用于连通性问题,比如社交网络中的好友关系判断等。
363+
364+
### 1.11 布隆过滤器(Bloom Filter)
365+
366+
- 一种基于位数组和哈希函数的概率型数据结构。
367+
- 用于快速判断某元素是否在集合中,但存在误判。
368+
369+
布隆过滤器的代码较复杂,一般需要使用多个哈希函数,可以使用现成的库来实现。

0 commit comments

Comments
 (0)