Skip to content

Conversation

@t-ooka
Copy link
Owner

@t-ooka t-ooka commented Sep 5, 2025

else:
stack.append(char)

return stack == []
Copy link

Choose a reason for hiding this comment

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

こちらは空のリストはfalseであることを利用して以下のようにも書けますね

Suggested change
return stack == []
return not stack

ご参考までにGoogleのスタイルガイドではこの書き方が推奨されているようです

For sequences (strings, lists, tuples), use the fact that empty sequences are false, so if seq: and if not seq: are preferable to if len(seq): and if not len(seq): respectively.
https://google.github.io/styleguide/pyguide.html#2144-decision

stack = []
bracket_pairs = { "}": "{", "]": "[", ")": "("}

for char in s:
Copy link

Choose a reason for hiding this comment

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

charはC/C++の予約語なので気にする人もいるかもしれません
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.34fedgfeaxcw

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

Choose a reason for hiding this comment

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

この書き方はぱっと見で対応が取れているか分かりづらいので、開き括弧→閉じ括弧にしてスタックからポップした開き括弧をこの対応に入れて一致を見るという方法もあります

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。たしかに対応見にくいですね、!
こちらのコミットで仰るやり方も試してみました。addc28c


せっかくなのでナイーブな方法を考えてみた。
全部 replace すればいいかという安易な考えでコーディングしてみた。
Brute Force で解いてみても計算量は O(N) なので悪くはないかなとは思った。(正規表現でマッチングしているのでもちろん計算量がアルゴリズムの選択肢になるとは思っていないです。)
Copy link

Choose a reason for hiding this comment

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

validな文字列だと括弧のペアがN/2あるのでループはN/2回回り、そしてinやreplaceで文字列の長さ分は計算量がかかりそうなのでO(N^2)かかるんじゃないかという気がしました

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかに、最悪の場合O(N) の処理が O(N/2) 回繰り返されるので O(N^2) ですね、!
考えが足りてなかったです、ありがとうございます!


for char in s:
if char in bracket_pairs:
if len(stack) > 0 and stack[-1] == bracket_pairs[char]:
Copy link

Choose a reason for hiding this comment

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

popしたものを見ると条件分岐が減らせるかなと思いました

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

Choose a reason for hiding this comment

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

ここの変数名、close_to_openとかにすると対応関係がわかりやすくなるかなと感じました。
辞書やタプルの場合は、x_to_y のような名前の付け方があるらしいです。
https://github.com/irohafternoon/LeetCode/pull/11/files#r2024946024

s = s.replace("{}", "")
s = s.replace("[]", "")

return s == ""
Copy link

Choose a reason for hiding this comment

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

return not s

と、 Implicit false を使った書き方はいかがでしょうか?

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/pyguide.html#2144-decision

For sequences (strings, lists, tuples), use the fact that empty sequences are false, so if seq: and if not seq: are preferable to if len(seq): and if not len(seq): respectively.

上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。


for char in s:
if char in bracket_pairs:
if len(stack) > 0 and stack[-1] == bracket_pairs[char]:
Copy link

Choose a reason for hiding this comment

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

if stack and stack[-1] == bracket_pairs[char]:

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

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.

5 participants