-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack.js
More file actions
60 lines (51 loc) · 1.04 KB
/
stack.js
File metadata and controls
60 lines (51 loc) · 1.04 KB
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
class Stack{
constructor(){
this.items = []
}
push(item){
this.items.push(item)
}
pop(){
return this.items.pop()
}
}
// function(){
// ()=>{
// {}
// }
// 匹配括号语法
// [1,2,3]
// ({(())({})}) 匹配的
// ({(()){})}) 不匹配了
// 判定给的符号,是不是括号匹配正确
// ({(())({})})
// [(,{,(,] (,
function isBlance(symbol){
const stack = new Stack()
const left = '({'
const right =')}'
let popValue
let blance = true
for(let i=0;i<symbol.length;i++){
let s = symbol[i]
if(left.includes(s)){
stack.push(s)
}else if(right.includes(s)){
let popValue = stack.pop()
match(popValue, s)
}
}
function match(popValue, current){
if(left.indexOf(popValue) !==right.indexOf(current)){
// 不匹配了
blance = false
}
}
return blance
}
console.log(isBlance("(({{}}))"))
console.log(isBlance("(({{}()}))"))
console.log(isBlance("(({{}))"))
console.log(isBlance("(()=>{return ()=>{}})}"))
// html规范校验 (jsx解析)
// 表达式计算