Skip to content

Create 20. Valid Parentheses.md - #8

Open
mt2324 wants to merge 2 commits into
mainfrom
20.-Valid-Parentheses
Open

mt2324 wants to merge 2 commits into
mainfrom
20.-Valid-Parentheses

Conversation

@mt2324

@mt2324 mt2324 commented Sep 5, 2026

Copy link
Copy Markdown
Owner

https://leetcode.com/problems/valid-parentheses/description/

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

Example 4:

Input: s = "([])"

Output: true

Example 5:

Input: s = "([)]"

Output: false

Constraints:

1 <= s.length <= 104
s consists of parentheses only '()[]{}'.

if c in open_to_close:
open_brackets.append(c)
continue
if open_brackets and open_to_close[open_brackets.pop()] == c:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

二つ目を別のif節の中に入れたほうが好みです(副作用のあるpopして大丈夫なの?と不安になりました、結局その場合 continueを踏んでFalseを返すのだと、続きを読めばわかりますが)

別のif節でも、stack[-1]で先頭を確認してから pop するほうが素直かな、と

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、popがif節の中に現れるとちょっと不安ですね。ありがとうございます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants