【发布时间】:2023-01-28 08:13:47
【问题描述】:
我正在尝试使用 JavaScript 从 leetcode 中找出有效的括号问题,但我无法找出解决此问题的计划。
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
我目前的思考过程是这样的:
- 将字符串拆分为数组(示例:“{}” --> [“{”,“}”,“[”,“]”,“(”,“)”]
- 遍历数组
- 使用每个字符的索引来比较...?
- 在此之后不确定...
请帮忙。
【问题讨论】:
-
要解决此问题,请使用堆栈。这会很容易。
-
如果您想找到解决方案,我建议您参考讨论部分。
标签: javascript