Skip to content

Commit 79dd4dd

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

File tree

4 files changed

+237
-3
lines changed

4 files changed

+237
-3
lines changed

.vitepress/config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default defineConfig({
6060
{ text: "前端工程化", link: "/interview/FrontendEngineering/index" },
6161
{ text: "Vue2/3框架", link: "/interview/Vue/index" },
6262
{ text: "React框架", link: "/interview/React/index" },
63-
{ text: "算法&数据结构", link: "/interview/Algorithm/index" },
63+
{ text: "算法&数据结构", link: "/interview/Algorithm/dataStructure" },
6464
{ text: "PWA应用", link: "/interview/PWA/index" },
6565
{ text: "微前端", link: "/interview/MicroFrontEnd/index" },
6666
{ text: "低代码", link: "/interview/LowCode/index" },

.vitepress/theme/sidebarOptions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ export const sideBarData = [
3737
collapsed: false,
3838
collapseDepth: 2,
3939
},
40+
{
41+
documentRootPath: "/docs",
42+
scanStartPath: "interview/Algorithm",
43+
resolvePath: "interview/Algorithm/",
44+
useTitleFromFileHeading: true,
45+
useTitleFromFrontmatter: true,
46+
frontmatterTitleFieldName: "title",
47+
hyphenToSpace: true,
48+
underscoreToSpace: true,
49+
collapsed: false,
50+
collapseDepth: 2,
51+
},
4052
{
4153
documentRootPath: "/docs",
4254
scanStartPath: "interview/JavaScript",

docs/interview/Algorithm/index.md renamed to docs/interview/Algorithm/dataStructure.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: 前端面试 数据结构与算法
2+
title: 数据结构概念
33
tags: front interview
44
theme: solarized-dark
55
---
66

7-
# 前端面试 数据结构与算法
7+
# 数据结构概念
88

99
## 1. 常见的数据结构有哪些?
1010

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
---
2+
title: 手写算法题合集
3+
tags: front interview
4+
theme: solarized-dark
5+
---
6+
7+
# LeetCode 经典算法题目整理
8+
9+
## 1. 两数之和
10+
11+
```javascript
12+
function twoSum(nums, target) {
13+
if (nums.length <= 1) return [];
14+
const map = new Map();
15+
for (let i = 0; i < nums.length; i++) {
16+
if (map.has(target - nums[i])) {
17+
return [map.get(target - nums[i]), i];
18+
} else {
19+
map.set(nums[i], i);
20+
}
21+
}
22+
return [];
23+
}
24+
```
25+
26+
## 2. 最大子序和
27+
28+
```javascript
29+
function maxSubArray(nums) {
30+
let prev = 0;
31+
let max = nums[0];
32+
nums.forEach((value) => {
33+
prev = Math.max(prev + value, value);
34+
max = Math.max(prev, max);
35+
});
36+
return max;
37+
}
38+
```
39+
40+
## 3. 反转链表
41+
42+
```javascript
43+
function reverseList(head) {
44+
let prev = null;
45+
let current = head;
46+
while (current) {
47+
const next = current.next;
48+
current.next = prev;
49+
prev = current;
50+
current = next;
51+
}
52+
return prev;
53+
}
54+
```
55+
56+
## 4. 比较版本号
57+
58+
```javascript
59+
function compareVersion(version1, version2) {
60+
const arr1 = version1.split(".");
61+
const arr2 = version2.split(".");
62+
let maxLength = Math.max(arr1.length, arr2.length);
63+
for (let i = 0; i < maxLength; i++) {
64+
if (Number(arr1[i]) > Number(arr2[i])) {
65+
return 1;
66+
}
67+
if (Number(arr1[i]) < Number(arr2[i])) {
68+
return -1;
69+
}
70+
if (i === maxLength - 1) {
71+
return 0;
72+
}
73+
}
74+
}
75+
```
76+
77+
## 5. 合并两个有序数组
78+
79+
```javascript
80+
function mergeSortArray(nums1, m, nums2, n) {
81+
let i = m - 1;
82+
let j = n - 1;
83+
let k = m + n - 1;
84+
while (i >= 0 || j >= 0) {
85+
if (i < 0) nums1[k--] = nums2[j--];
86+
if (j < 0) nums1[k--] = nums1[i--];
87+
if (nums1[i] < nums2[j]) nums1[k--] = nums2[j--];
88+
else nums1[k--] = nums1[i--];
89+
}
90+
}
91+
```
92+
93+
## 6. 无重复字符的最长子串
94+
95+
```javascript
96+
function lengthOfLongestSubstring(s) {
97+
let max = 0;
98+
let startIndex = 0;
99+
let map = new Map();
100+
101+
for (let i = 0; i < s.length; i++) {
102+
if (map.has(s[i])) {
103+
startIndex = map.get(s[i]) + 1;
104+
}
105+
max = Math.max(i - startIndex + 1, max);
106+
map.set(s[i], i);
107+
}
108+
return max;
109+
}
110+
```
111+
112+
## 7. 有效的括号
113+
114+
```javascript
115+
function validParenthesis(s) {
116+
const map = {
117+
"}": "{",
118+
"]": "[",
119+
")": "(",
120+
};
121+
const stack = [];
122+
123+
for (let i = 0; i < s.length; i++) {
124+
if (stack.length && stack[stack.length - 1] === map[s[i]]) {
125+
stack.pop();
126+
} else {
127+
stack.push(s[i]);
128+
}
129+
}
130+
return !stack.length;
131+
}
132+
```
133+
134+
## 8. LRU 缓存机制
135+
136+
```javascript
137+
class LRUCache {
138+
constructor(capacity) {
139+
this.capacity = capacity; // 缓存的最大容量
140+
this.cache = new Map(); // 使用 Map 存储缓存数据
141+
}
142+
143+
// 获取缓存数据
144+
get(key) {
145+
if (!this.cache.has(key)) {
146+
return -1; // 如果缓存中没有该数据,则返回 -1
147+
}
148+
// 如果数据存在,先删除再重新插入(表示该数据被访问过,更新为最近使用)
149+
const value = this.cache.get(key);
150+
this.cache.delete(key);
151+
this.cache.set(key, value);
152+
return value;
153+
}
154+
155+
// 设置缓存数据
156+
put(key, value) {
157+
// 如果缓存已满,删除最旧的数据
158+
if (this.cache.size >= this.capacity) {
159+
// Map 的 keys() 返回插入顺序,删除第一个元素
160+
this.cache.delete(this.cache.keys().next().value);
161+
}
162+
// 如果已经存在这个 key,删除旧值并插入新值
163+
if (this.cache.has(key)) {
164+
this.cache.delete(key);
165+
}
166+
// 插入新数据到缓存
167+
this.cache.set(key, value);
168+
}
169+
}
170+
171+
// 测试代码
172+
const lru = new LRUCache(2);
173+
lru.put(1, 1); // 缓存 {1=1}
174+
lru.put(2, 2); // 缓存 {1=1, 2=2}
175+
console.log(lru.get(1)); // 返回 1,缓存 {2=2, 1=1}
176+
lru.put(3, 3); // 缓存已满,删除最久未使用的 2,缓存 {1=1, 3=3}
177+
console.log(lru.get(1)); // 返回 -1 (未找到)
178+
lru.put(4, 4); // 缓存 {3=3, 4=4}
179+
console.log(lru.get(2)); // 返回 -1 (未找到)
180+
console.log(lru.get(3)); // 返回 3
181+
console.log(lru.get(4)); // 返回 4
182+
```
183+
184+
## 9. 买卖股票的最佳时机
185+
186+
```javascript
187+
function maxProfit(prices) {
188+
let minPrice = prices[0];
189+
let maxProfit = 0;
190+
for (let i = 1; i < prices.length; i++) {
191+
minPrice = Math.min(prices[i], minPrice);
192+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
193+
}
194+
return maxProfit;
195+
}
196+
```
197+
198+
## 10. 最长回文子串
199+
200+
```javascript
201+
function longestPalindrome(s) {
202+
let l = 0;
203+
let r = 0;
204+
205+
for (let i = 0; i < s.length; i++) {
206+
helper(i, i);
207+
helper(i, i + 1);
208+
}
209+
210+
function helper(m, n) {
211+
while (m >= 0 && n < s.length && s[m] == s[n]) {
212+
m--;
213+
n++;
214+
}
215+
if (n - m > r - l) {
216+
r = n;
217+
l = m;
218+
}
219+
}
220+
return s.slice(l + 1, r);
221+
}
222+
```

0 commit comments

Comments
 (0)