Skip to content

Commit a558653

Browse files
add: 系列手写题
1 parent 29ec007 commit a558653

5 files changed

+164
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @lc app=leetcode.cn id=3 lang=javascript
3+
*
4+
* [3] 无重复字符的最长子串
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @return {number}
11+
*/
12+
var lengthOfLongestSubstring = function(s) {
13+
14+
};
15+
// @lc code=end
16+
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
1+
const timeout = (order, time) => new Promise(resolve => setTimeout(resolve(), time))
2+
13
class Scheduler {
24
constructor() {
35
this.maxCount = 2
6+
this.runCount = 0
47
this.queue = []
58
this.run = []
69
}
7-
8-
add(task) {
9-
this.queue.push(task)
10+
addTask(timer, task) {
11+
this.queue.push(timeout(task, timer).then(() => console.log(task)))
1012
}
11-
schedule() {
12-
if(this.run.length < this.count && this.queue.length) {
13-
const task = this.queue.shift()
14-
const
13+
start() {
14+
for (let i = 0; i < this.maxCount; i++) {
15+
this.request()
1516
}
1617
}
17-
}
18+
request() {
19+
if(!this.queue || !this.queue.length || this.runCount >= this.maxCount) {
20+
return
21+
}
22+
this.runCount++
23+
24+
this.queue.shift().then(() => {
25+
this.runCount--
26+
this.request()
27+
})
28+
}
29+
}
30+
31+
32+
33+
const scheduler = new Scheduler()
34+
35+
scheduler.addTask(1000, '1');
36+
scheduler.addTask(500, '2');
37+
scheduler.addTask(300, '3');
38+
scheduler.addTask(400, '4');
39+
40+
scheduler.start()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 并发promise
3+
*
4+
* @param {*} array 待执行的函数列表
5+
* @param {*} limit 并发限制
6+
*/
7+
async function asyncPool(array, limit) {
8+
// 存储Promise的执行结果
9+
const res = []
10+
// 正在执行的promise
11+
const executing = []
12+
13+
for (const item of array) {
14+
const p = item
15+
res.push(p)
16+
if(limit <= array.length) {
17+
p.then(() => executing.splice(0, 1))
18+
executing.push(p)
19+
if(executing.length >= limit) await Promise.race(executing)
20+
}
21+
}
22+
23+
return Promise.all(res)
24+
}
25+
26+
const timeout = (order, time) => new Promise(resolve => setTimeout(resolve(console.log(order)), time))
27+
28+
const arr = [
29+
timeout(1, 100),
30+
timeout(2, 200),
31+
timeout(3, 300),
32+
timeout(4, 400),
33+
timeout(5, 500),
34+
timeout(6, 500),
35+
timeout(7, 400),
36+
timeout(8, 300),
37+
timeout(9, 200),
38+
timeout(10, 100),
39+
]
40+
asyncPool(arr, 10).then(result => console.log(result))
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// 使用递归的方式实现
2+
const data = [
3+
{ id: '01', name: '张大大', pid: '', job: '项目经理' },
4+
{ id: '02', name: '小亮', pid: '01', job: '产品leader' },
5+
{ id: '03', name: '小美', pid: '01', job: 'UIleader' },
6+
{ id: '04', name: '老马', pid: '01', job: '技术leader' },
7+
{ id: '05', name: '老王', pid: '01', job: '测试leader' },
8+
{ id: '06', name: '老李', pid: '01', job: '运维leader' },
9+
{ id: '07', name: '小丽', pid: '02', job: '产品经理' },
10+
{ id: '08', name: '大光', pid: '02', job: '产品经理' },
11+
{ id: '09', name: '小高', pid: '03', job: 'UI设计师' },
12+
{ id: '10', name: '小刘', pid: '04', job: '前端工程师' },
13+
{ id: '11', name: '小华', pid: '04', job: '后端工程师' },
14+
{ id: '12', name: '小李', pid: '04', job: '后端工程师' },
15+
{ id: '13', name: '小赵', pid: '05', job: '测试工程师' },
16+
{ id: '14', name: '小强', pid: '05', job: '测试工程师' },
17+
{ id: '15', name: '小涛', pid: '06', job: '运维工程师' }
18+
]
19+
20+
// 用reduce实现方式
21+
// function toTree(arr, parentId){
22+
// function dp(parId) {
23+
// return arr.reduce((acc, cur) => {
24+
// if(cur.pid === parId) {
25+
// cur.children = dp(cur.id)
26+
// acc.push(cur)
27+
// }
28+
// return acc
29+
// }, [])
30+
// }
31+
// return dp(parentId)
32+
// }
33+
34+
// 用for循环
35+
function toTree(arr, parentId) {
36+
function dp(parId) {
37+
let res = []
38+
for (let i = 0; i < arr.length; i++) {
39+
const item = arr[i];
40+
if(item.pid === parId) {
41+
item.children = dp(item.id)
42+
res.push(item)
43+
}
44+
}
45+
return res
46+
}
47+
return dp(parentId)
48+
}
49+
50+
51+
console.log(toTree(data, ''))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
输入: s = "abcabcbb"
3+
输出: 3
4+
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
5+
*/
6+
/**
7+
* @param {string} s
8+
* @return {number}
9+
*/
10+
var lengthOfLongestSubstring = function(s) {
11+
let myMap = new Map()
12+
let l = 0, r = 0, maxl = 0
13+
while(r < s.length) {
14+
if(myMap.has(s[r])) {
15+
let i = myMap.get(s[r]) + 1
16+
l = Math.max(l, i)
17+
}
18+
myMap.set(s[r], r)
19+
maxl = Math.max(maxl, r - l + 1)
20+
r++
21+
}
22+
return maxl
23+
}
24+
25+
var s = "abcabcbb"
26+
console.log(lengthOfLongestSubstring(s))

0 commit comments

Comments
 (0)