Skip to content

Conversation

@skypenguins
Copy link
Owner

20. Valid Parentheses

問題リンク: https://leetcode.com/problems/valid-parentheses/
言語: Python

次回予告: 35. Search Insert Position

@skypenguins skypenguins self-assigned this Jul 8, 2025
if len(s) <= 1:
return False

visited_chars = deque()
Copy link

@h1rosaka h1rosaka Jul 9, 2025

Choose a reason for hiding this comment

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

(step2,3含め)dequeではなくlistでも良いのかなと思いました。(popとappendしかしてなさそうなため。)

Copy link

Choose a reason for hiding this comment

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

コードの動きの上では変わらないと思いますが、適切なコンテナを使ったほうが意図が伝わりやすいか思います。

if actual_bracket != expected_bracket:
return False
else:
unmatched_open_brackets.append(bracket)
Copy link

Choose a reason for hiding this comment

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

(好みの問題かもしれませんが)、ひだりカッコの処理が先にあったくれた方が個人的には頭に入りやすいかもと思いました。(左かっこの方がシンプルなので、シンプルなものが先に片付いてくれて方が、最後に残っているより少し楽に感じるため)

```python
class Solution:
def isValid(self, s: str) -> bool:
closing_to_opening_brackets = {")": "(", "]": "[", "}": "{"}
Copy link

Choose a reason for hiding this comment

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

opening_to_closing_brackets = {"(": ")", "[": "]", "{": "}"}

と、開きカッコを先に持ってきたほうが読みやすいと思います。ロジックが少し変わりますが、過去のコードレビューを参照すれば見つかると思います。

class Solution:
def isValid(self, s: str) -> bool:
closing_to_opening_bracket = {")": "(", "]": "[", "}": "{"}
unmatched_brackets = deque()
Copy link

Choose a reason for hiding this comment

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

全体的に読みやすいと思います!個人的にはstep2のようにunmatched_open_bracketsと明示してくれた方が読むのが楽に思いました。

unmatched_brackets = deque()

for bracket in s:
if bracket in closing_to_opening_bracket:
Copy link

Choose a reason for hiding this comment

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

93行目の処理を先に書いたほうが読みやすいと思いました。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.9kpbwslvv3yv

Choose a reason for hiding this comment

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

+1 です

closing_to_opening_bracket = {")": "(", "]": "[", "}": "{"}
unmatched_brackets = deque()

for bracket in s:
Copy link

Choose a reason for hiding this comment

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

問題の制約で起きえないですが、文字列にかっこ以外の文字が含まれる可能性を考えると、bracketとして良いのだろうか、という気持ちになりました。

unmatched_brackets = deque()

for bracket in s:
if bracket in closing_to_opening_bracket:

Choose a reason for hiding this comment

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

+1 です

if len(s) <= 1:
return False

visited_chars = deque()

Choose a reason for hiding this comment

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

deque である必要はないというのはその通りでして、追加すると collections から import しているというのは明記したいですね (LeetCode だとデフォルトで import されているので)。

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants