Skip to content

Commit

Permalink
feat: 0322
Browse files Browse the repository at this point in the history
  • Loading branch information
niaogege committed Mar 23, 2024
1 parent 9707ac5 commit e042260
Show file tree
Hide file tree
Showing 14 changed files with 920 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/interview/experiences/15202309handwriting.md
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ function isCycleDfs(obj) {
if (cache.has(val)) {
return true;
}
// 如果不是引用类型直接跳过
if (typeof val !== 'object' || val == null) continue;
cache.add(val);
let flag = helper(val);
Expand Down
89 changes: 89 additions & 0 deletions docs/interview/experiences/18middleHandwriting.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,113 @@ nav:
## 1.大数相乘

```js
function multiple(a, b) {
let [m, n] = [a.length, b.length];
let res = new Array(m + n).fill(0);
for (let i = m - 1; i >= 0; i--) {
for (let j = n - 1; j >= 0; j--) {
let p1 = i + j;
let p2 = p1 + 1;
let tmp = Number(a[i]) * Number(b[j]);
let data = res[p2] + tmp;
res[p2] = data % 10;
res[p1] = Math.floor(data / 10) + res[p1];
}
}
while (res[0] === 0) {
res.shift();
}
return res.length ? res.join('') : '0';
}
multiple('22', '22');
```

## 2.数字转汉字

## 3.归并排序/堆排序

## 4.数组转树

```js

```

## 5.树转数组

## 6.判断对象是否存在循环引用

```js
function isCycleObj(obj) {
let cache = new Set();
let dfs = (obj) => {
let vals = Object.values(val);
for (val of vals) {
if (cache.has(val)) {
return true;
}
if (typeof val != 'object' || val == null) continue;
cache.add(val);
if (dfs(val)) {
return true;
}
}
return false;
};
return dfs(obj);
}
```

## 7.抢红包算法

```js
function redPacket(total, num, max = 2, min = '0.1') {
function name(params) {
let remain = total;
let ans = [];
for (let i = 0; i < num - 1; i++) {
let Max = (remain / num) * max;
let cur = Math.floor(Max * Math.random() * 100) / 100;
cur = cur < min ? min : cur;
ans.push(cur);
remain = Math.round((remain - cur) * 100) / 100;
}
ans.push(remain);
return ans;
}
redPacket(10, 4);
}
```

## 8.封装异步的 fetch,使用 async await 方式来使用

## 9.查找文章中出现频率最高的单词

## 10.实现双向数据绑定

## 11.判断两个数组内容是否相同

```js
function isSameArr(a, b) {
if (a.length != b.length) return false;
let m = new Map();
// a塞到m
for (let item of a) {
if (m.has(item)) {
m.set(item, m.get(item) + 1);
} else {
m.set(item, 1);
}
}
for (let item of b) {
let val = m.get(item);
if (val == undefined || val < 1) return false;
m.set(item, val - 1);
}
return true;
}
```

## 链接

- [手写 js](https://juejin.cn/post/6946136940164939813?searchId=2024031814180491A668E2D8A6BD15EEE9#heading-55)
13 changes: 9 additions & 4 deletions docs/interview/experiences/21leetcodeA.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ function outPutStr(str) {
## 2.反转链表

```js
/**
* @param {ListNode} head
* @return {ListNode}
*/
// bfs
var reverseList = function (head) {
var pre = null;
var cur = head;
Expand All @@ -148,6 +145,14 @@ var reverseList = function (head) {
}
return pre;
};
// dfs
function reverseList(head) {
if (head == null || head.next == null) return head;
let last = reverseList(head.next);
head.next.next = head;
head.next = null;
return last;
}
```

## 3.LRU 缓存机制
Expand Down
13 changes: 13 additions & 0 deletions docs/interview/experiences/22leetcodeB.md
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,19 @@ function noMerge(nums) {
}
return count;
}
// 右区间
function noMerge2(arr) {
arr.sort((a, b) => a[1] - b[1]);
let end = arr[0][1];
let count = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i][0] >= end) {
count++;
end = arr[i][1];
}
}
return arr.length - count;
}
```

## [32.寻找两个正序数组的中位数](https://leetcode.cn/problems/median-of-two-sorted-arrays/description/)
Expand Down
103 changes: 102 additions & 1 deletion docs/interview/experiences/23leetcodeC.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ nav:
* 21.分割回文串
* 22.划分字母区间
* 23.多数元素
* 24.反转链表II
* 25.判断两个数组内容相等
*/
```

Expand Down Expand Up @@ -97,7 +99,38 @@ function removeNode(node) {

## 6.k 个一组反转链表

> 一级难!!!
> 一级难!!!还是不能理解
```js
function reverseLink(head, k) {
let dummy = { next: head, val: null };
let pre = dummy;
let cur = head;
let len = 0;
while (head) {
len++;
head = head.next;
}
let count = Math.floor(len / k);
for (let i = 0; i < count; i++) {
for (let j = 0; j < k - 1; j++) {
// pre-1-2-3-4
// pre-2-1-3-4
// 记录2
let next = cur.next;
// 1指向 3
cur.next = cur.next.next;
// 2指向 1
next.next = pre.next;
// 头节点指向2
pre.next = next;
}
pre = cur;
cur = pre.next;
}
return dummy.next;
}
```

## [7.计算质数的个数](https://leetcode.cn/problems/count-primes/)

Expand All @@ -121,6 +154,45 @@ function countPrime(n) {
}
```

## 9.打乱数组

```js
function shufttle(arr) {
for (let i = arr.length - 1; i >= 0; i--) {
let tmp = Math.floor(Math.random() * (i + 1));
[arr[i], arr[tmp]] = [arr[tmp], arr[i]];
}
return arr;
}
shufttle([1, 2, 3, 4, 5, 6]);
```

## 10.复原 IP 地址

```js
// 0000 => [0.0.0.0]
function reverseIp(s) {
let ans = [];
let backTrack = (path, start) => {
if (path.length == 4 && start == s.length) {
ans.push(path.slice().join('.'));
return;
}
for (let i = start; i < s.length; i++) {
let cur = s.slice(start, i + 1);
if (+cur > 255 || cur.length > 3) continue;
if (cur.length > 1 && cur[0] == '0') continue;
path.push(cur);
backTrack(path, i + 1);
path.pop();
}
};
backTrack([], 0);
return ans;
}
reverseIp('0000');
```

## 12.两两交换链表节点

```js
Expand Down Expand Up @@ -153,3 +225,32 @@ var swapPairs = function (head) {
## 19.小孩报数问题

有 30 个小孩儿,编号从 1-30,围成一圈依此报数,1、2、3 数到 3 的小孩儿退出这个圈, 然后下一个小孩 重新报数 1、2、3,问最后剩下的那个小孩儿的编号是多少?

## 24.反转链表 II

> 跟 k 个一组链表反转 好容易混淆
```js
// 1->2->3->4->5 2,4
// 1->4->3->3->5
function reverseLink(head, left, right) {
let dummy = {
next: head,
val: null,
};
let pre = dummy;
let i = 1;
while (i < left) {
pre = pre.next;
i++;
}
let cur = pre.next;
for (let i = 0; i < right - left; i++) {
let next = cur.next;
cur.next = next.next;
next.next = pre.next;
pre.next = next;
}
return dummy.next;
}
```
Loading

0 comments on commit e042260

Please sign in to comment.