Nav: << previous: 19.删除链表的倒数第 N 个结点 | next: 21.合并两个有序链表 >>
Description
tab: English
<p>Given a string <code>s</code> containing just the characters <code>'('</code>, <code>')'</code>, <code>'{'</code>, <code>'}'</code>, <code>'['</code> and <code>']'</code>, determine if the input string is valid.</p>
<p>An input string is valid if:</p>
<ol>
<li>Open brackets must be closed by the same type of brackets.</li>
<li>Open brackets must be closed in the correct order.</li>
<li>Every close bracket has a corresponding open bracket of the same type.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "()"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "()[]{}"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "(]"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "([])"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p><strong class="example">Example 5:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "([)]"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of parentheses only <code>'()[]{}'</code>.</li>
</ul>
> [!tip]- Hint 1
>
> Use a stack of characters.
> [!tip]- Hint 2
>
> When you encounter an opening bracket, push it to the top of the stack.
> [!tip]- Hint 3
>
> When you encounter a closing bracket, check if the top of the stack was the opening for it. If yes, pop it from the stack. Otherwise, return false.
---
[submissions](https://leetcode.com/problems/valid-parentheses/submissions/) | [solutions](https://leetcode.com/problems/valid-parentheses/solutions/)
tab: 中文
<p>给定一个只包括 <code>'('</code>,<code>')'</code>,<code>'{'</code>,<code>'}'</code>,<code>'['</code>,<code>']'</code> 的字符串 <code>s</code> ,判断字符串是否有效。</p>
<p>有效字符串需满足:</p>
<ol>
<li>左括号必须用相同类型的右括号闭合。</li>
<li>左括号必须以正确的顺序闭合。</li>
<li>每个右括号都有一个对应的相同类型的左括号。</li>
</ol>
<p> </p>
<p><strong class="example">示例 1:</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "()"</span></p>
<p><span class="example-io"><b>输出:</b>true</span></p>
</div>
<p><strong class="example">示例 2:</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "()[]{}"</span></p>
<p><span class="example-io"><b>输出:</b>true</span></p>
</div>
<p><strong class="example">示例 3:</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "(]"</span></p>
<p><span class="example-io"><b>输出:</b>false</span></p>
</div>
<p><strong class="example">示例 4:</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "([])"</span></p>
<p><span class="example-io"><b>输出:</b>true</span></p>
</div>
<p><strong class="example">示例 5:</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "([)]"</span></p>
<p><span class="example-io"><b>输出:</b>false</span></p>
</div>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> 仅由括号 <code>'()[]{}'</code> 组成</li>
</ul>
> [!tip]- 提示 1
>
> Use a stack of characters.
> [!tip]- 提示 2
>
> When you encounter an opening bracket, push it to the top of the stack.
> [!tip]- 提示 3
>
> When you encounter a closing bracket, check if the top of the stack was the opening for it. If yes, pop it from the stack. Otherwise, return false.
---
[提交记录](https://leetcode.cn/problems/valid-parentheses/submissions/) | [题解](https://leetcode.cn/problems/valid-parentheses/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122