-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
20180817 七颗葡萄汇聚 #1
Labels
周会
每周记录
Comments
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
let map = {
')': '(',
']': '[',
'}': '{'
}
let stack = []
for (let i = 0; i < s.length; i++) {
let c = s[i]
if (c === '(' || c === '[' || c === '{') {
stack.push(c)
} else if (c === ')' || c === ']' || c === '}') {
if (map[c] === stack[stack.length - 1]) {
stack.pop()
} else {
return false
}
}
}
return stack.length === 0
} |
|
|
/**
* @param {string} str
* @return {boolean}
*/
const PARENTHESIS_MAP = {
')': '(',
']': '[',
'}': '{'
}
function isValid(str) {
let stack = [];
for (let e of str) {
switch (e) {
case ('('): case ('{'): case ('['):
stack.push(e);
break;
case (')'): case ('}'): case (']'):
if (PARENTHESIS_MAP[e] !== stack.pop()) return false;
break;
default:
break;
}
}
return !stack.length;
}
isValid('[]][');
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LeetCode 20题 Valid Parentheses
给一个字符串,包括'(', ')', '{', '}', '[' 和‘]’ 判断是否是正确组成顺序,例如“()”和“(){}[]”是正确的,但是“(]”和"([)]"错误。
The text was updated successfully, but these errors were encountered: