-
Notifications
You must be signed in to change notification settings - Fork 108
/
检查字串里面的括号是否有正确嵌套 .js
49 lines (48 loc) · 1.21 KB
/
检查字串里面的括号是否有正确嵌套 .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
/*
请用 javascript 实现一个函数 verify(text), 检查字串里面的括号是否有正确嵌套
例如:
verify("---(++++)----") -> 1
verify("") -> 1
verify("before ( middle []) after ") -> 1
verify(") (") -> 0
verify("} {") -> 1 //no, this is not a mistake.
verify("<( >)") -> 0
verify("( [ <> () ] <> )") -> 1
verify(" ( [)") -> 0
*/
function couple(str) {
switch (str) {
case ")":
return "(";
case "}":
return "{";
case "{":
return "}";
case "]":
return "[";
case ">":
return "<";
default:
return str;
}
}
function verify(str) {
var strs = str.match(/[\(\)\{\}\[\]\<\>]/g);
var ret = [];
var item = "";
if (strs) {
for (var i = 0; i < strs.length; i++) {
if (ret.length > 0 && couple(strs[i]) == ret[ret.length - 1]) {
item = ret.pop();
continue;
}
ret.push(strs[i]);
}
}
if (ret.length) {
console.log(0);
} else {
console.log(1);
}
}
verify("before ( middle []) after ")