-
Notifications
You must be signed in to change notification settings - Fork 0
20. Valid Parentheses #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
91de100
3fbad19
51b1d98
9beaf9b
0855e06
379f114
addc28c
c865bca
6c2940c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # step1 | ||
|
|
||
| どこかで同じ問題をやっていた? | ||
| 記憶があって stack に積まれた最後の閉じ括弧を対応するペアと比較する方法がすっと出てきた。 | ||
|
|
||
| # step2 | ||
|
|
||
| せっかくなのでナイーブな方法を考えてみた。 | ||
| 全部 replace すればいいかという安易な考えでコーディングしてみた。 | ||
| ~~Brute Force で解いてみても計算量は O(N) なので悪くはないかなとは思った。~~(正規表現でマッチングしているのでもちろん計算量がアルゴリズムの選択肢になるとは思っていないです。) | ||
|
|
||
| 追記: 時間計算量は、最悪で O(N) の処理が O(N/2) 回ループするので、 O(N^2)になりそうと指摘いただきました。 | ||
|
|
||
| ## step3 | ||
|
|
||
| 同じようにコーディングをした。step1から変わっていない。改善を考えるのが苦手かもしれない。 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
| bracket_pairs = { ")": "(", "]": "[", "}": "{"} | ||
|
|
||
| for char in s: | ||
| if char in bracket_pairs: | ||
| if len(stack) > 0 and stack[-1] == bracket_pairs[char]: | ||
| stack.pop() | ||
| else: | ||
| return False | ||
| else: | ||
| stack.append(char) | ||
|
|
||
| return len(stack) == 0 | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| while "()" in s or "{}" in s or "[]" in s: | ||
| s = s.replace("()", "") | ||
| s = s.replace("{}", "") | ||
| s = s.replace("[]", "") | ||
|
|
||
| return s == "" | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
| bracket_pairs = { ")": "(", "]": "[", "}": "{"} | ||
|
|
||
| for char in s: | ||
| if char in bracket_pairs: | ||
| if len(stack) > 0 and stack[-1] == bracket_pairs[char]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if stack and stack[-1] == bracket_pairs[char]:と書いたほうがシンプルだと思います。 |
||
| stack.pop() | ||
| else: | ||
| return False | ||
| else: | ||
| stack.append(char) | ||
|
|
||
| return len(stack) == 0 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| while "()" in s or "{}" in s or "[]" in s: | ||
| s = s.replace("()", "") | ||
| s = s.replace("{}", "") | ||
| s = s.replace("[]", "") | ||
|
|
||
| return s == "" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。 |
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||
| class Solution: | ||||||
| def isValid(self, s: str) -> bool: | ||||||
| stack = [] | ||||||
| bracket_pairs = { "}": "{", "]": "[", ")": "("} | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この書き方はぱっと見で対応が取れているか分かりづらいので、開き括弧→閉じ括弧にしてスタックからポップした開き括弧をこの対応に入れて一致を見るという方法もあります
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。たしかに対応見にくいですね、! |
||||||
|
|
||||||
| for char in s: | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. charはC/C++の予約語なので気にする人もいるかもしれません |
||||||
| if char in bracket_pairs: | ||||||
| if len(stack) > 0 and stack[-1] == bracket_pairs[char]: | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. popしたものを見ると条件分岐が減らせるかなと思いました |
||||||
| stack.pop() | ||||||
| else: | ||||||
| return False | ||||||
| else: | ||||||
| stack.append(char) | ||||||
|
|
||||||
| return stack == [] | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらは空のリストはfalseであることを利用して以下のようにも書けますね
Suggested change
ご参考までにGoogleのスタイルガイドではこの書き方が推奨されているようです
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| while "()" in s or "{}" in s or "[]" in s: | ||
| s = s.replace("()", "") | ||
| s = s.replace("{}", "") | ||
| s = s.replace("[]", "") | ||
| return not s |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
| opening_to_closing = { "(": ")", "{": "}", "[": "]"} | ||
| for bracket in s: | ||
| if bracket in opening_to_closing: | ||
| stack.append(opening_to_closing[bracket]) | ||
| continue | ||
| else: | ||
| if not stack or bracket != stack.pop(): | ||
| return False | ||
| return not stack |
There was a problem hiding this comment.
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