-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlink.js
118 lines (111 loc) · 2.04 KB
/
link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//单向列表
class LinkedList{
constructor(){
this.head = null
}
insert(node){//O(1)
if(this.head!==null){
node.next = this.head
}
this.head = node
}
find(node){//))O(n)
let p = this.head
while(p&&p!==node){
p = p.next()
}
return p
}
}
//队列
class Queue {
enqueue(item){
if(this.size===this.max){
throw 'Queue Overflow'
}
this.data[this.p++] = item
this.size++
if(this.p == this.max){
this.p = 0
}
}
dequeue(){
if(this.size==0){
throw 'Queue Underflow'
}
const item = this.data[this.q++]
this.size--
if(this.q===this.max){
this.q=0
}
return item
}
}
//反转二叉树
function reverseBTress(node){
if(!node){
return
}
const tmp = node.left
node.left = node.right
node.right = tmp
reverseBTress(node.right)
reverseBTress(node.left)
}
//a.name=ramroll&a.dress&x=1&y=
function parse(str){
return str.split('&').reduce((o,kv)=>{
const [key,value] = kv.split('=')
if(!value){
return o
}
// o[key] = value
deep_set(o,kv.split(/[\[\]]/g).filter(x=>x),value)
return o
},{})
}
function deep_set(o,path,value){
debugger
let i = 0;
for(;i<path.length-1;i++){
if(o[path[i]]===undefined){
if(path[i+1].match(/^\d+$/)){
o[path[i]]=[]
}else{
o[path[i]]={}
}
}
o=o[path[i]]//第一次O.a和O指向了同一个地址,从此以后O怎么变,O.a也是一样的
}
o[path[i]] = decodeURIComponent(value)
}
console.log(parse('a[name]=fox&a[company]=teach&b=why'))
console.log(parse('a[0]=1&a[1]=2&b=why'))
console.log(parse('a&b&c'))
console.log(parse('color=deep%20Bule'))
//两个栈模仿队列
class Queue{
constructor(){
this.s1 = []
this.s2 = []
}
//入队
enqueue(item){
this.s1.push(item)
}
//出队
dequeue(){
while(this.s1.length>0){
this.s2.push(this.s1.pop())
}
if(this.s2.length>0){
return this.s2.pop()
}
}
}
//
function f(w,h){
const dp = []
for(let y = 0;y<=h;y++){
}
}