Wanwan87 20. valid parentheses#6
Conversation
| - ループでi番目とi+1番目の入力を比較して、開きかっこがきたら閉じるかっこが来るまでi+2,i+3と進めていく | ||
| - 例えば入力が([])だったら、i番目:( i+1番目:[ でfalseなので ( を保持しして 一致する閉じかっこが来るまでループを回す | ||
| - などを考えていたが、入力が(([]))のときなどの判断が難しかったので、一旦別の方法に切り替える | ||
| - (([]))のときに 最初の(を0番目としたら、入力の一番最後である5番目に)が来ればよい。1番目の ( に対応する ) はstrの長さ-1番目に、[ に対応する ] は strの長さ-2番目に来る。鏡のように対称の位置にあるかっこを探す形であれば実現できそう |
There was a problem hiding this comment.
同じ失敗しました。
上手くいかなかったのは()(]を考えてないからなんですね。やっと理由がわかりました。
失敗の反省など書いてるの良いと思います。
There was a problem hiding this comment.
ありがとうございます、良いアイデアだ!と思ってコードを書き始めちゃいましたが、(問題文で提示されている)テストケースは少なくとも試しておいたほうが良さそうですね
| ```python3 | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| i=0 |
There was a problem hiding this comment.
二項演算子の両側にスペースが入っているものと入っていないものが混ざっているのが気になりました。
参考までにスタイルガイドへのリンクを共有いたします。
https://peps.python.org/pep-0008/#other-recommendations
Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <=, >=, in, not in, is, is not), Booleans (and, or, not).
https://google.github.io/styleguide/pyguide.html#s3.6-whitespace
Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。
| def isValid(self, s: str) -> bool: | ||
| i=0 | ||
| # stackに格納していく | ||
| a=[] |
There was a problem hiding this comment.
a という変数名からは、中にどのような値が格納されるか想像しにくく感じます。 open_brackets 等、中に格納される値が想像しやすい変数名を付けるとよいと思います。
There was a problem hiding this comment.
ありがとうございます、中身がわかる変数名を意識します
この問題:https://leetcode.com/problems/valid-parentheses/
次の問題:https://leetcode.com/problems/reverse-linked-list/description/