Skip to content

020-valid-parentheses#7

Merged
05ryt31 merged 2 commits into
mainfrom
feat/020
Jan 6, 2026
Merged

020-valid-parentheses#7
05ryt31 merged 2 commits into
mainfrom
feat/020

Conversation

@05ryt31
Copy link
Copy Markdown
Owner

@05ryt31 05ryt31 commented Jan 5, 2026

問題リンク

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

問題文の概要

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.

次に解く予定の問題

https://leetcode.com/problems/reverse-linked-list/description/

elif "{}" in s:
s = s.replace("{}", "")

if s: return True
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 s: return False
else: return True

が正しいように思います。

また、 Implicit False を用いて、

return not s

と書いたほうがシンプルだと思います。

Comment thread 020-valid-parentheses/step3.py Outdated
class Solution:
def isValid(self, s: str) -> bool:
stack = []
closeToOpen = {")": "(", "]": "[", "}": "{"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

以下コメントをご参照ください。
tNita/arai60#3 (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.

snake_case意識します!
ありがとうございます

Comment thread 020-valid-parentheses/step3.py Outdated
class Solution:
def isValid(self, s: str) -> bool:
stack = []
closeToOpen = {")": "(", "]": "[", "}": "{"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

open_to_close のほうが、見た目の違和感が少ないように感じます。ほかの方でこのように書かれている方がいらっしゃいます。ぜひ参考にしてみてください。

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.

修正いたしました!

## 別解
class Solution:
def isValid(self, s: str) -> bool:
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.

個人的には変数名に、どのようなデータ構造として使うかより、中にどのような値が含まれているかを付けたほうが、読み手に取って理解の助けになると思います。 open_brackets はいかがでしょうか?

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.

毎回こういった変数に名前をつけるのが苦手で良い命名が思いつかないです。。。
参考になります

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には変数名に、どのようなデータ構造として使うかより、中にどのような値が含まれているかを付けたほうが、読み手に取って理解の助けになる

私も同意です。

class Solution:
def isValid(self, s: str) -> bool:
while s:
if "()" in s:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

競技プログラミング同好会の hmuta 氏が、このようなコードを書かれていたのを思い出しました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、チェックせずにどんどん replace し、短くならなくなったら False という方法はできることはできます。
計算量は2乗ですが、Native コードなのでそんなに遅くない気がします。

@05ryt31 05ryt31 merged commit 9e487fb into main Jan 6, 2026
1 check passed
Copy link
Copy Markdown

@TakayaShirai TakayaShirai left a comment

Choose a reason for hiding this comment

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

レビュー後に回答の修正をする場合は、前のファイルを修正するのではなく、step4.py などのファイルを追加する方式をおすすめします!修正後にレビューをした場合に、過去のコメントとコードが一致しておらずわかりにくくなる場合があります!

Comment on lines +20 to +23
while s:
if "()" in s:
s = s.replace("()", "")
elif "[]" in s:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この解法は自分では思いついていなかったため、参考になりました!

Comment on lines +5 to +6
open_to_close = {"()": ")", "[]": "]", "{": "}"}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

前回のコミット(baadf67) で、open_to_closeに余計な括弧が入り込んでしまっているみたいです。

また、close_to_open から open_to_close にする場合は以下の変更を加える必要があると思います。

  • if c in open_to_close の方で stack に追加
  • else の方で検証を行う必要。

これは、valid な出力の場合 open → close の順に括弧がくるためです。具体的には、if c in open_to_closeで検証を行う方法だと、入力が ()の場合、最初に(がきますが、検証を行う際に close_brackets の中身がまだ空なため False になります。

たとえば、以下のような書き方があると思います。

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

        for c in s:
            if c in open_to_close:
                close_bracktes.append(open_to_close[c])
            else:
                if not close_bracktes:
                  return False
                expected_bracket = close_bracktes.pop()
                if c != expected_bracket:
                    return False
        return not close_bracktes

## 別解
class Solution:
def isValid(self, s: str) -> bool:
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.

個人的には変数名に、どのようなデータ構造として使うかより、中にどのような値が含まれているかを付けたほうが、読み手に取って理解の助けになる

私も同意です。

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.

4 participants