Skip to content

20 valid parentheses#7

Open
MA-yo-TA wants to merge 4 commits into
mainfrom
20-Valid-Parentheses
Open

20 valid parentheses#7
MA-yo-TA wants to merge 4 commits into
mainfrom
20-Valid-Parentheses

Conversation

@MA-yo-TA

@MA-yo-TA MA-yo-TA commented Jun 8, 2026

Copy link
Copy Markdown
Owner

stack.append(character)
continue

if character in closes:

@huyfififi huyfififi Jun 8, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

確かに [aiu](eo) のような入力を想定するのは納得感があり、そうするとここでも if 文が必要になるのですね、勉強になります 👀
なんとなくですが、カッコ以外の文字列をスキップすることを先に持ってきてもいいかなと思いました。メインで想定している入力以外のものを最初にはじけば、後の処理は限られた範囲内の入力でわかりやすいかな、と。

        for character in s:
            if character not in opens and character not in closes:
                continue

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.

ありがとうございます。そうですね、今のコードだとカッコじゃない文字の扱いが implicit なので冒頭で明示した方がわかりやすいかもですね。

class Solution:
def isValid(self, s: str) -> bool:
opens = ["(", "[", "{"]
close_to_open = {")": "(", "]": "[", "}": "{"}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
silby72/LeetCode_arai60#5 (comment)

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.

ありがとうございます。順番が気持ち悪いなと思いつつロジックを優先してこう定義しましたが、開き→閉じで定義しても特段ロジックが複雑になるわけではないのでそちらの方がいいかもしれないですね。

def isValid(self, s: str) -> bool:
opens = ["(", "[", "{"]
close_to_open = {")": "(", "]": "[", "}": "{"}
stack = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
silby72/LeetCode_arai60#5 (comment)

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.

ありがとうございます。
今回は opens (開きカッコの集まり)を変数で置いるのでそれをやめて スタックを open_brackets とするか、あるいは (found|discovered)_open_brackets などと書いて区別するか、でしょうかね。

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.

3 participants