Skip to content

Commit

Permalink
feat: 0306
Browse files Browse the repository at this point in the history
  • Loading branch information
niaogege committed Mar 6, 2024
1 parent a9d017f commit 6783d6f
Show file tree
Hide file tree
Showing 13 changed files with 688 additions and 21 deletions.
10 changes: 8 additions & 2 deletions docs/interview/browser/webCache.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ web 缓存主要指的是两部分:浏览器缓存和 http 缓存。

占内存(有些缓存会被存到内存中)

### 基本原理

- 1. 浏览器在加载资源时,根据请求头的 expires 和 cache-control 判断是否命中强缓存,如果命中则直接从缓存系统读取资源,不会发请求到服务器
- 2. 如果没有命中强缓存,浏览器一定会发送一个请求到服务器,通过 Last-modified 和 Etag 验证资源是否命中协商缓存,如果命中,服务器会将这个请求返回,但是不会返回这个资源的数据,依然是从缓存系统中读取资源
- 3. 如果前面两者都没有命中,直接从服务器加载资源

### http 之强缓存

http 缓存分为两种缓存,强制缓存和协商缓存, 我们来深度剖析一下强制缓存和协商缓存各自的优劣以及他们的使用场景以及使用原理
Expand Down Expand Up @@ -114,7 +120,7 @@ private 则表示资源只能在客户端被缓存,拒绝资源在代理服务
### http 之协商缓存

#### 基于 last-modified 的协商缓存
#### 基于 last-modified/if-modified-since 的协商缓存

基于 last-modified 的协商缓存实现方式是:

Expand All @@ -134,7 +140,7 @@ last-modified 和 if-Modified-Since 缺点 1.因为是根据文件修改时间

了解决上述的这两个问题。从 http1.1 开始新增了一个头信息,ETag

#### Etag/if-none-match
#### 基于 Etag/if-none-match 协商缓存

ETag 就是将原先协商缓存的比较时间戳的形式修改成了比较**文件指纹**

Expand Down
123 changes: 123 additions & 0 deletions docs/interview/experiences/12interview0703.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,129 @@ const arr = [
parentId: 4,
},
];
[
{
id: 0,
name: '',
children: [
{
id: 2,
name: '部门B',
children: [
{
id: 1,
name: '部门A',
children: [
{
id: 3,
name: '部门C',
children: [],
},
],
},
{
id: 5,
name: '部门D',
children: [],
},
],
},
],
},
];

function arrToTree(arr) {
let ans = [];
var dfs = (arr, res, parentId) => {
for (let item of arr) {
if (parentId == item.parentId) {
const one = {
...item,
children: [],
};
res.push(one);
dfs(arr, one.children, item.id);
delete item.children;
}
}
};
dfs(arr, ans, 0);
return ans;
}
```

树转数组

```js
var listTree = [
{
id: 1,
name: '部门1',
pid: 0,
children: [
{
id: 2,
name: '部门1-1',
pid: 1,
children: [
{
id: 4,
name: '部门1-1-1',
pid: 2,
children: [],
},
],
},
{
id: 3,
name: '部门1-2',
pid: 1,
children: [
{
id: 5,
name: '部门1-2-1',
pid: 3,
children: [],
},
],
},
],
},
{
id: 6,
name: '部门2',
pid: 0,
children: [
{
id: 7,
name: '部门2-1',
pid: 6,
children: [],
},
],
},
{
id: 8,
name: '部门3',
pid: 0,
children: [],
},
];
function treeToArr(tree) {
let ans = [];
var dfs = (arr, res) => {
for (let child of arr) {
res.push(child);
if (child.children.length) {
dfs(child.children, res);
}
delete child.children;
}
};
dfs(tree, ans);
return ans;
}
treeToArr(listTree);
```

- Node 是怎么部署的? pm2 守护进程的原理?
Expand Down
89 changes: 86 additions & 3 deletions docs/interview/experiences/21leetcodeA.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ var reverseList = function (head) {

## 3.LRU 缓存机制

```js
class LRU {
constructor(limit) {
this.limit = limit;
this.cache = new Map();
}
get(key) {
if (this.cache.has(key)) {
const val = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, val);
} else {
return -1;
}
}
set(key, val) {
const size = this.cache.size;
if (this.cache.has(key)) {
this.cache.delete(key);
} else {
if (size >= this.limit) {
const oldKey = this.cache.keys().next().value;
this.cache.delete(oldKey);
}
}
this.cache.set(key, val);
}
}
```

## 4.数组中的第 K 个最大元素

```js
Expand All @@ -160,6 +190,8 @@ var reverseList = function (head) {

## 5.K 个一组链表反转

> 不会!
```js
var reverseKGroup = function (head, k) {
var dummy = {
Expand Down Expand Up @@ -316,6 +348,8 @@ var mergeTwoLists = function (l1, l2) {
};
```

## 9.两数之和

## [10.最长回文子串](https://leetcode.cn/problems/longest-palindromic-substring/)

> [参考解析](https://leetcode.cn/problems/longest-palindromic-substring/solutions/9001/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-bao-gu/)
Expand Down Expand Up @@ -497,7 +531,20 @@ function allPermute(arr) {
allPermute([1, 2, 3]);
```

## 19.二叉树的最近公共祖先
## [19.二叉树的最近公共祖先](https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/description/)

```js
// 最近公共祖先
var lowestCommonAncestor = function (root, p, q) {
if (root == null || p == root || q == root) return root;
let left = lowestCommonAncestor(root.left, p, q);
let right = lowestCommonAncestor(root.right, p, q);
if (right != null && left != null) return root;
if (left != null) return left;
if (right != null) return right;
return null;
};
```

## 20.手撕快速排序

Expand Down Expand Up @@ -568,6 +615,42 @@ var zigzagLevelOrder = function (root) {
};
```

## 23.螺旋矩阵

```js
function matrix(arr) {
if (!arr.length) return [];
let up = 0;
let down = arr.length - 1;
let left = 0;
let right = arr[0].length - 1;
let ans = [];
while (true) {
// 从左到右
for (let i = left; i <= right; i++) {
ans.push(arr[up][i]);
}
if (++up > down) break;
// 从上到下
for (let i = up; i <= down; i++) {
ans.push(arr[i][right]);
}
if (left > --right) break;
// 从右往左
for (let i = right; i >= left; i--) {
ans.push(arr[down][i]);
}
if (up > --down) break;
// 从下往上
for (let i = down; i >= up; i--) {
ans.push(arr[i][left]);
}
if (++left > right) break;
}
return ans;
}
```

## 24.相交链表

```js
Expand All @@ -576,7 +659,7 @@ function insectionLink(headA, headB) {
var set = new Set();
while (cur) {
set.add(cur);
cur = cur.mext;
cur = cur.next;
}
cur = headB;
while (cur) {
Expand Down Expand Up @@ -666,7 +749,7 @@ function water(nums) {
let curTop = stack.slice(-1); // 当前栈顶元素
let h = Math.min(cur, nums[curTop]) - nums[top];
let w = i - curTop - 1;
res = res + h * w;
res += +h * w;
}
}
stack.push(i);
Expand Down
2 changes: 2 additions & 0 deletions docs/interview/experiences/23leetcodeHotA.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ nav:
* 1.二叉树的最小深度和最大深度
* 2.最长公共前缀
* 3.删除升序链表中重复出现的所有节点
* 4.数组转树
* 5.树转数组
*/
```

Expand Down
Loading

0 comments on commit 6783d6f

Please sign in to comment.