20 valid parentheses#7
Conversation
| stack.append(character) | ||
| continue | ||
|
|
||
| if character in closes: |
There was a problem hiding this comment.
確かに [aiu](eo) のような入力を想定するのは納得感があり、そうするとここでも if 文が必要になるのですね、勉強になります 👀
なんとなくですが、カッコ以外の文字列をスキップすることを先に持ってきてもいいかなと思いました。メインで想定している入力以外のものを最初にはじけば、後の処理は限られた範囲内の入力でわかりやすいかな、と。
for character in s:
if character not in opens and character not in closes:
continueThere was a problem hiding this comment.
ありがとうございます。そうですね、今のコードだとカッコじゃない文字の扱いが implicit なので冒頭で明示した方がわかりやすいかもですね。
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| opens = ["(", "[", "{"] | ||
| close_to_open = {")": "(", "]": "[", "}": "{"} |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
silby72/LeetCode_arai60#5 (comment)
There was a problem hiding this comment.
ありがとうございます。順番が気持ち悪いなと思いつつロジックを優先してこう定義しましたが、開き→閉じで定義しても特段ロジックが複雑になるわけではないのでそちらの方がいいかもしれないですね。
| def isValid(self, s: str) -> bool: | ||
| opens = ["(", "[", "{"] | ||
| close_to_open = {")": "(", "]": "[", "}": "{"} | ||
| stack = [] |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
silby72/LeetCode_arai60#5 (comment)
There was a problem hiding this comment.
ありがとうございます。
今回は opens (開きカッコの集まり)を変数で置いるのでそれをやめて スタックを open_brackets とするか、あるいは (found|discovered)_open_brackets などと書いて区別するか、でしょうかね。
この問題:https://leetcode.com/problems/valid-parentheses/
次の問題:https://leetcode.com/problems/reverse-linked-list/