Skip to content

Commit 24bfd73

Browse files
feat: 新增笔记
1 parent 36d8be2 commit 24bfd73

File tree

9 files changed

+272
-50
lines changed

9 files changed

+272
-50
lines changed

.DS_Store

6 KB
Binary file not shown.

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"liveServer.settings.port": 5501
3+
}

course/course-04-01/queue.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const queue = [];
2+
queue.push(100);
3+
queue.push(200);
4+
const item1 = queue.shift();
5+
const item2 = queue.shift();

course/course-04-01/笔记.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### 4.1 队列简介
2+
3+
#### 一、队列是什么?
4+
5+
> 队列是一个`先进先出`的数据结构。
6+
7+
> Javascript 中没有队列,但可以用 Array 实现队列的所有功能。

course/course-04-02/笔记.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
### 4.2 什么场景用队列
2+
3+
> 一、需要先进先出的场景用队列
4+
5+
#### 场景一、食堂排队打饭可以使用队列
6+
7+
#### 场景二、JS 异步中的任务队列
8+
9+
1. JS 是单线程,无法同时处理异步中的并发任务;
10+
2. 使用任务队列先后处理异步任务;
11+
12+
#### 场景三、计算最近请求次数(算法题)
13+
14+
##### 1. 题目描述
15+
16+
```
17+
获取3000毫秒内的最近的请求次数
18+
19+
输入:
20+
["RecentCounter", "ping", "ping", "ping", "ping"]
21+
[[], [1], [100], [3001], [3002]]
22+
输出:
23+
[null, 1, 2, 3, 3]
24+
25+
解释:
26+
RecentCounter recentCounter = new RecentCounter();
27+
recentCounter.ping(1); // requests = [1],范围是 [-2999,1],返回 1
28+
recentCounter.ping(100); // requests = [1, 100],范围是 [-2900,100],返回 2
29+
recentCounter.ping(3001); // requests = [1, 100, 3001],范围是 [1,3001],返回 3
30+
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002],范围是 [2,3002],返回 3
31+
```
32+
33+
##### 2. 解题思路
34+
35+
1. 有新请求就入队,3000ms 前发出的请求出队;
36+
2. 队列的长度就是最近请求次数;

course/course-04-03/queue.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const RecentCounter = function () {
2+
this.q = [];
3+
};
4+
5+
/**
6+
* @param {number} t
7+
* @return {number}
8+
*/
9+
RecentCounter.prototype.ping = function (t) {
10+
this.q.push(t);
11+
while (this.q[0] < t - 3000) {
12+
this.q.shift();
13+
}
14+
15+
return this.q.length;
16+
};

course/course-04-03/笔记.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
### 4.3 (leetCode 算法题) 最近的请求次数
2+
3+
#### 1. 题目 LeetCode 地址:[最近的请求次数](https://leetcode-cn.com/problems/number-of-recent-calls/)
4+
5+
#### 2. 题目描述
6+
7+
写一个  RecentCounter  类来计算特定时间范围内最近的请求。
8+
9+
请你实现 RecentCounter 类:
10+
11+
RecentCounter() 初始化计数器,请求数为 0 。
12+
int ping(int t) 在时间 t 添加一个新请求,其中 t 表示以毫秒为单位的某个时间,并返回过去 3000 毫秒内发生的所有请求数(包括新请求)。确切地说,返回在 [t-3000, t] 内发生的请求数。
13+
保证 每次对 ping 的调用都使用比之前更大的 t 值。
14+
15+
示例:
16+
17+
```
18+
输入:
19+
["RecentCounter", "ping", "ping", "ping", "ping"]
20+
[[], [1], [100], [3001], [3002]]
21+
输出:
22+
[null, 1, 2, 3, 3]
23+
24+
解释:
25+
RecentCounter recentCounter = new RecentCounter();
26+
recentCounter.ping(1); // requests = [1],范围是 [-2999,1],返回 1
27+
recentCounter.ping(100); // requests = [1, 100],范围是 [-2900,100],返回 2
28+
recentCounter.ping(3001); // requests = [1, 100, 3001],范围是 [1,3001],返回 3
29+
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002],范围是 [2,3002],返回 3
30+
```
31+
32+
提示:
33+
34+
1 <= t <= 109
35+
保证每次对 ping 调用所使用的 t 值都 严格递增
36+
至多调用 ping 方法 104 次
37+
38+
#### 3. 解题思路
39+
40+
1. 题目的实际含义是,返回数组中每一次输入 3000 范围内的数量,如:输入 3001,数组中 3001 在 3000 范围内的元素包括:1、100、3001,所以 3000 范围内的数量为 3;
41+
2. 通过题目描述的示例可以看出,发出的请求越早,越不在最近 3000ms 内的请求里;
42+
3. 根据队列先进先出的特性,把每一次请求入队,不在 3000ms 内的请求出队,统计队列的长度就可以得到最近请求次数;
43+
44+
#### 4. 解题步骤
45+
46+
1. 新建一个队列用于存放每一次输入的元素;
47+
2. 把每一次请求入队;
48+
3. 遍历队列,把超出 3000ms 的请求出队;
49+
4. 返回队列的长度;
50+
51+
#### 5. 解题代码(见同级目录文件:queue.js)
52+
53+
#### 6. 分析算法的时间复杂度和空间复杂度
54+
55+
1. 时间复杂度:queue.js 中仅有一个 while 循环,所以时间复杂度为 O(n);
56+
2. 空间复杂度:queue.js 中,遍历的所有元素都可能存储在队列中,所以空间复杂度最大为 O(n);

demo/1.时间复杂度-demo.html

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,98 @@
11
<script>
2-
/** 时间复杂度O(1) */
3-
function fnO1() {
4-
console.log("时间复杂度O(1)");
2+
// /** 时间复杂度O(1) */
3+
// function fnO1() {
4+
// console.log("时间复杂度O(1)");
55

6-
const a = 5;
7-
const b = 10;
8-
const sum = a + b;
9-
}
6+
// const a = 5;
7+
// const b = 10;
8+
// const sum = a + b;
9+
// }
1010

11-
/** 时间复杂度 O(logn)、 O(nlogn) */
11+
// /** 时间复杂度 O(logn)、 O(nlogn) */
1212

13-
function fnOlogn(n) {
14-
console.log("时间复杂度O(logn)");
13+
// function fnOlogn(n) {
14+
// console.log("时间复杂度O(logn)");
1515

16-
let count = 0;
17-
let i = 1;
18-
while (i < n) {
19-
i = i * 2;
20-
count++;
21-
}
16+
// let count = 0;
17+
// let i = 1;
18+
// while (i < n) {
19+
// i = i * 2;
20+
// count++;
21+
// }
2222

23-
console.log("时间复杂度O(logn), 执行次数count: ", count);
24-
}
23+
// console.log("时间复杂度O(logn), 执行次数count: ", count);
24+
// }
2525

26-
function fnOnlogn(n) {
27-
console.log("时间复杂度O(nlogn)");
28-
let count = 0;
29-
for (let j = 0; j < n; j++) {
30-
let i = 1;
31-
while (i < n) {
32-
i = i * 2;
33-
count++;
34-
}
35-
}
36-
console.log("时间复杂度O(nlogn), 执行次数count: ", count);
37-
}
26+
// function fnOnlogn(n) {
27+
// console.log("时间复杂度O(nlogn)");
28+
// let count = 0;
29+
// for (let j = 0; j < n; j++) {
30+
// let i = 1;
31+
// while (i < n) {
32+
// i = i * 2;
33+
// count++;
34+
// }
35+
// }
36+
// console.log("时间复杂度O(nlogn), 执行次数count: ", count);
37+
// }
3838

39-
/** 时间复杂度 fnO(m+n), 当可以确认n >= m的时候,时间复杂度可以修改为 O(n) */
39+
// /** 时间复杂度 fnO(m+n), 当可以确认n >= m的时候,时间复杂度可以修改为 O(n) */
4040

41-
function fnOmn1(m, n) {
42-
let countM = 0;
43-
let countN = 0;
44-
for (let i = 0; i < m; i++) {
45-
countM += 1;
46-
}
47-
for (let j = 0; j < n; j++) {
48-
countN += 1;
49-
}
41+
// function fnOmn1(m, n) {
42+
// let countM = 0;
43+
// let countN = 0;
44+
// for (let i = 0; i < m; i++) {
45+
// countM += 1;
46+
// }
47+
// for (let j = 0; j < n; j++) {
48+
// countN += 1;
49+
// }
5050

51-
console.log("时间复杂度O(m+n),执行次数count: ", countM + countN);
51+
// console.log("时间复杂度O(m+n),执行次数count: ", countM + countN);
52+
// }
53+
54+
// /** 时间复杂度 fnO(m*n), 当可以确认n >= m的时候,时间复杂度可以修改为 O(n^2) */
55+
// function fnOmn2(m, n) {
56+
// let count = 0;
57+
// for (let i = 0; i < m; i++) {
58+
// for (let j = 0; j < n; j++) {
59+
// count += 1;
60+
// }
61+
// }
62+
63+
// console.log("时间复杂度O(m*n),执行次数count: ", count);
64+
// }
65+
66+
function array2List(arr, type = 0) {
67+
if (!arr.length) return null;
68+
let header = { data: arr[0], next: null };
69+
let obj = header;
70+
for (let i = 1; i < arr.length; i++) {
71+
obj.next = { data: arr[i], next: null };
72+
obj = obj.next;
73+
}
74+
if (type) obj.next = header;
75+
return header;
5276
}
5377

54-
/** 时间复杂度 fnO(m*n), 当可以确认n >= m的时候,时间复杂度可以修改为 O(n^2) */
55-
function fnOmn2(m, n) {
56-
let count = 0;
57-
for (let i = 0; i < m; i++) {
58-
for (let j = 0; j < n; j++) {
59-
count += 1;
60-
}
78+
var list = array2List([1, 2, 3, 4, 5]);
79+
console.log(list);
80+
81+
var reverseList = function (head) {
82+
console.log(head);
83+
let p1 = head;
84+
let p2 = null;
85+
while (p1) {
86+
const tmp = p1.next;
87+
p1.next = p2;
88+
p2 = p1;
89+
p1 = tmp;
90+
// p2 = p1;
91+
// p1 = p1.next;
6192
}
93+
return p2;
94+
};
6295

63-
console.log("时间复杂度O(m*n),执行次数count: ", count);
64-
}
96+
var result = reverseList(list);
97+
console.log(result);
6598
</script>

demo/3.linked-list.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// /** 模拟链表 */
2+
3+
// const a = { val: "a" };
4+
// const b = { val: "b" };
5+
// const c = { val: "c" };
6+
// const d = { val: "d" };
7+
// const e = { val: "e" };
8+
9+
// /** 创建链表 */
10+
// a.next = b;
11+
// b.next = c;
12+
// c.next = d;
13+
// d.next = e;
14+
15+
// /** 插入元素 */
16+
// const f = { val: "f" };
17+
// c.next = f;
18+
// f.next = d;
19+
20+
// /** 删除元素 */
21+
// b.next = f;
22+
23+
// /** 遍历链表 */
24+
// let p = a;
25+
// while (p) {
26+
// console.log(p.val);
27+
// p = p.next;
28+
// }
29+
30+
// function ListNode(val, next) {
31+
// this.val = val === undefined ? 0 : val;
32+
// this.next = next === undefined ? null : next;
33+
// }
34+
35+
// function arrayToLinkedList(array) {
36+
// if (!Array.isArray(array)) {
37+
// return '传入的参数必须是数组'
38+
// } else if (array.length = 0) {
39+
// return "传入的参数必须是数组长度不能为0";
40+
// } else {
41+
// let nodeList = {val: array[0]};
42+
// let length = array.length
43+
// while (length) {
44+
// const node = {
45+
// val: array[1],
46+
// }
47+
// nodeList.next = node;
48+
// array.shift();
49+
// }
50+
// }
51+
// }
52+
53+
function array2List(arr, type = 0) {
54+
if (!arr.length) return null;
55+
let header = { data: arr[0], next: null };
56+
let obj = header;
57+
for (let i = 1; i < arr.length; i++) {
58+
obj.next = { data: arr[i], next: null };
59+
obj = obj.next;
60+
}
61+
if (type) obj.next = header;
62+
return header;
63+
}
64+
65+
var list = array2List([1, 2, 3, 4]);
66+
console.log(list);

0 commit comments

Comments
 (0)