Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shfshanyue committed Mar 18, 2022
1 parent efc23e0 commit f528f7b
Show file tree
Hide file tree
Showing 55 changed files with 6,756 additions and 2,822 deletions.
2 changes: 1 addition & 1 deletion .vuepress/theme/components/Bar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</div>
<div class="bar-intro">
<div class="text">
每天晚上九点 <a href="https://space.bilibili.com/28696526">B站讲解前端工程化直播</a>,并解答相关问题。
<a href="https://www.apifox.cn/?utm_source=shanyue-question">Apifox,在国内可替代 Postman 并强烈适用于团队的 API 管理工具。</a>
</div>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@

+ [前端大厂面试指南](https://codepen.io/collection/MggMed?grid_type=list)

## 特别赞助

[**Apifox: API 文档、API 调试、API Mock、API 自动化测试**](https://www.apifox.cn?utm_source=shanyue-question)

## 大厂内推

添加微信 `shanyue94`,免费大厂内推。
Expand Down
28 changes: 28 additions & 0 deletions base/algorithm/177.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,31 @@ description: "【Q176】如何在数组中找出三个数之和为N 字节跳动
::: tip Issue
欢迎在 Gtihub Issue 中回答此问题: [Issue 177](https://github.com/shfshanyue/Daily-Question/issues/177)
:::

::: tip Author
回答者: [HNHED](https://github.com/HNHED)
:::

排序之后使用双指针
let ar = [5, 12, 6, 3, 9, 2, 1, 7];

function getthreenum(arr, target, result = []) {
arr = arr.sort((a, b) => a - b)
const len = arr.length;
for (let i = 0; i < len; i++) {
let j = i + 1;
let k = len - 1;
while (j < k) {
if (arr[j] + arr[k] > target - arr[i]) {
k--;
} else if (arr[j] + arr[k] < target - arr[i]) {
j++;
} else {
result.push([arr[i], arr[j], arr[k]]);
j++;
}
}
}
return result;
}
console.log(getthreenum(ar, 13, []));
54 changes: 54 additions & 0 deletions base/algorithm/419.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,57 @@ const encode = (str) => {
return encodedArray.join("");
};
```
::: tip Author
回答者: [chenxinan](https://github.com/chenxinan)
:::
```javascript
function solution(s, limit) {
const n = s.length;
let res = "";
for (let i = 0, cnt = 0; i < n; i += cnt) {
cnt = 1;
while (s[i] === s[i + cnt]) cnt++;
res += cnt > limit ? s[i] + cnt : s[i].repeat(cnt);
}
return res;
}
```
::: tip Author
回答者: [LiJinWD](https://github.com/LiJinWD)
:::
const encode = word => {
if(!word) return "";
let ary = word.split('');
let group = {}
let result = ""
group[ary[0]] = 1
for(let i = 1, j = ary.length; i <= j; i++) {
if(ary[i - 1] != ary[i]) {
result += Object.entries(group).flat().join('')
group = {}
group[ary[i]] = 1
} else {
group[ary[i]]++
}
}
return result
}
const encode1 = word => {
return word.replace(/1/gi, '')
}
const encode2 = word => {
let one = word.substring(0, 1)
let newWord = ''
for(item of word) {
newWord += item == 2 ? one : item
one = item
}
return newWord
}
96 changes: 96 additions & 0 deletions base/algorithm/49.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,99 @@ this.items = [];
return result;
}
}

::: tip Author
回答者: [someGenki](https://github.com/someGenki)
:::

基于最大堆实现优先队列

```js
class MaxHeap {
constructor(arr = []) {
this.heap = []; // 用数组表示堆结构
arr.forEach((item) => this.add(item));
}

add(value) {
// O(logK) 插入节点值: 放入数组末尾并上浮到合适位置
this.heap.push(value);
this.shiftUp(this.heap.length - 1);
}

pop() {
// O(logK) 提取最大值/堆顶: 提取 heap[0] 并用 heap[-1] 进行代替,然后从顶部开始下沉到合适位置
const max = this.heap[0];
this.swap(0, this.size() - 1);
this.heap.pop();
this.shiftDown(0);
return max;
}

peek() {
// 获取最值/堆顶
return this.heap[0];
}

size() {
// 获取当前堆大小
return this.heap.length;
}

// ↓私有属性↓
swap(index1, index2) {
// 交换节点位置
const temp = this.heap[index1];
this.heap[index1] = this.heap[index2];
this.heap[index2] = temp;
}

parentIndex(index) {
// 获取父节点的位置 (index - 1) / 2 向下取整
return (index - 1) >> 1;
}

leftChildIndex(index) {
// 获取左子节点
return index * 2 + 1;
}

rightChildIndex(index) {
// 获取右子节点
return index * 2 + 2;
}

shiftUp(index) {
// 上浮节点,当前值小于父节点值时停止,使当前堆保持最大堆的性质
let parentIndex = this.parentIndex(index);
while (index > 0 && this.heap[parentIndex] < this.heap[index]) {
this.swap(index, parentIndex);
parentIndex = this.parentIndex((index = parentIndex));
}
}

shiftDown(index) {
// 下沉节点,当前值大于子节点值时停止,使当前堆保持最大堆的性质
const leftIndex = this.leftChildIndex(index);
const rightIndex = this.rightChildIndex(index);
// 先比较左子节点值,当前值小于左子节点,则交换,并递归进行下沉
if (this.heap[index] < this.heap[leftIndex]) {
this.swap(leftIndex, index);
this.shiftDown(leftIndex);
}
if (this.heap[index] < this.heap[rightIndex]) {
this.swap(rightIndex, index);
this.shiftDown(rightIndex);
}
}
}

// ==TEST==
const priorityQueue = new MaxHeap([2, 5, 3]);
console.log(priorityQueue.peek()); // 5
priorityQueue.add(7);
console.log(priorityQueue.peek()); // 7
priorityQueue.pop();
priorityQueue.add(1);
console.log(priorityQueue.peek()); // 5
```
18 changes: 18 additions & 0 deletions base/algorithm/701.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,21 @@ description: "【Q682】100层楼,两个玻璃球,求最少多少次测出
最后,让我们把第一个鸡蛋没碎的情况下,所尝试的楼层数完整列举出来:

14,27, 39, 50, 60, 69, 77, 84, 90, 95, 99, 100

::: tip Author
回答者: [chenxinan](https://github.com/chenxinan)
:::

```javascript
function solution(n) {
const dp = Array.from({ length: 2 }, () => new Array(n + 1).fill(0));
for (let i = 1; i <= n; i++) {
dp[0][i] = i;
dp[1][i] = n;
for (let j = 1; j <= i; j++) {
dp[1][i] = Math.min(dp[1][i], Math.max(dp[0][j - 1], dp[1][i - j]) + 1);
}
}
return dp[1][n - 1];
}
```
14 changes: 14 additions & 0 deletions base/algorithm/711.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,17 @@ function rand7() {
}
}
```

::: tip Author
回答者: [chenxinan](https://github.com/chenxinan)
:::

```javascript
function rand5() {
return (Math.random() * 6) | 0;
}

function rand7() {
return (rand5() & 1) * 4 + (rand5() & 1) * 2 + (rand5() & 1);
}
```
6 changes: 6 additions & 0 deletions base/http/594.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ description: "【Q578】HTTP 响应头 cache-control: s-maxage=0 是什么意思
::: tip Issue
欢迎在 Gtihub Issue 中回答此问题: [Issue 594](https://github.com/shfshanyue/Daily-Question/issues/594)
:::

::: tip Author
回答者: [maya1900](https://github.com/maya1900)
:::

缓存立即失效
6 changes: 6 additions & 0 deletions base/network/121.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ ACK
:::

现在 UDP 也挺可靠的还快

::: tip Author
回答者: [changshou83](https://github.com/changshou83)
:::

@Carrie999 大佬细说
6 changes: 6 additions & 0 deletions base/network/175.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ description: "【Q174】TCP 三次握手发生在 socket 建立的哪一步 字
::: tip Issue
欢迎在 Gtihub Issue 中回答此问题: [Issue 175](https://github.com/shfshanyue/Daily-Question/issues/175)
:::

::: tip Author
回答者: [changshou83](https://github.com/changshou83)
:::

TCP Client 调用 connec()请求建立连接时
9 changes: 9 additions & 0 deletions base/network/183.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ tcp 协议之所以可靠 就是由于 三次握手四次挥手 建立的连接,
题主 把 两次挥手改成三次挥手了,我还是拿挂电话的例子
如果步骤 4 没了 就挂断,那么 B 不确定 A 是否收到 B 已经说完了, 而直接挂断
,如果 A 没收到 A 将会一直等待

::: tip Author
回答者: [changshou83](https://github.com/changshou83)
:::

第一次:客户端通知服务端我要结束了
第二次:服务端告诉客户端我知道了
第三次:服务端通知客户端我准备好了
第四次:客户端通知服务端表示我知道了
6 changes: 6 additions & 0 deletions base/network/184.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ description: "【Q183】tcp 中 time_wait 堆积过多会有什么问题,为
::: tip Issue
欢迎在 Gtihub Issue 中回答此问题: [Issue 184](https://github.com/shfshanyue/Daily-Question/issues/184)
:::

::: tip Author
回答者: [changshou83](https://github.com/changshou83)
:::

会占用大量网络资源(接口),导致网络响应卡顿。超时时间长是为了有足够的时间确保客户端能接受到消息
8 changes: 8 additions & 0 deletions base/network/55.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ description: "【Q054】简述TCP 的三次握手 字节跳动面试题、阿里
![](https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Tcp-handshake.svg/537px-Tcp-handshake.svg.png)

三次握手之后,客户端与服务器端的两个 socket 处于 `ESTABSHED` 状态

::: tip Author
回答者: [changshou83](https://github.com/changshou83)
:::

第一次:服务端知道客户端有发送能力
第二次:客户端知道服务端有发送和接受的能力
第三次:服务端知道客户端有接受能力
Loading

0 comments on commit f528f7b

Please sign in to comment.