We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
难度:简单 来源:20. 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 注意空字符串可被认为是有效字符串。
示例 1:
输入: "()" 输出: true
示例 2:
输入: "()[]{}" 输出: true
示例 3:
输入: "(]" 输出: false
示例 4:
输入: "([)]" 输出: false
示例 5:
输入: "{[]}" 输出: true
思路:
题解:
/** * @param {string} s * @return {boolean} */ var isValid = function(s) { let l = s.length if (l % 2 !== 0) return false let stack = [] let map = new Map([ ['(', ')'], ['[', ']'], ['{', '}'] ]) for (let i = 0; i < l; i++) { if (map.has(s[i])) { stack.push(s[i]) } else { if (stack.length === 0) return false if (map.get(stack[stack.length - 1]) === s[i]) stack.pop() else return false } } return stack.length === 0 };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
有效的括号
难度:简单
来源:20. 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
示例 2:
示例 3:
示例 4:
示例 5:
思路:
题解:
The text was updated successfully, but these errors were encountered: