Skip to content

Wanwan87 20. valid parentheses#6

Open
wanwan87 wants to merge 4 commits into
mainfrom
wanwan87-20.-Valid-Parentheses
Open

Wanwan87 20. valid parentheses#6
wanwan87 wants to merge 4 commits into
mainfrom
wanwan87-20.-Valid-Parentheses

Conversation

@wanwan87
Copy link
Copy Markdown
Owner

Comment thread 20. Valid Parentheses.md
- ループでi番目とi+1番目の入力を比較して、開きかっこがきたら閉じるかっこが来るまでi+2,i+3と進めていく
- 例えば入力が([])だったら、i番目:( i+1番目:[ でfalseなので ( を保持しして 一致する閉じかっこが来るまでループを回す
- などを考えていたが、入力が(([]))のときなどの判断が難しかったので、一旦別の方法に切り替える
- (([]))のときに 最初の(を0番目としたら、入力の一番最後である5番目に)が来ればよい。1番目の ( に対応する ) はstrの長さ-1番目に、[ に対応する ] は strの長さ-2番目に来る。鏡のように対称の位置にあるかっこを探す形であれば実現できそう
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

同じ失敗しました。
上手くいかなかったのは()(]を考えてないからなんですね。やっと理由がわかりました。
失敗の反省など書いてるの良いと思います。

Copy link
Copy Markdown
Owner Author

@wanwan87 wanwan87 May 12, 2026

Choose a reason for hiding this comment

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

ありがとうございます、良いアイデアだ!と思ってコードを書き始めちゃいましたが、(問題文で提示されている)テストケースは少なくとも試しておいたほうが良さそうですね

Comment thread 20. Valid Parentheses.md
```python3
class Solution:
def isValid(self, s: str) -> bool:
i=0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

二項演算子の両側にスペースが入っているものと入っていないものが混ざっているのが気になりました。

参考までにスタイルガイドへのリンクを共有いたします。

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 (+, -, *, /, //, %, **, @).

なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。

Comment thread 20. Valid Parentheses.md
def isValid(self, s: str) -> bool:
i=0
# stackに格納していく
a=[]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

a という変数名からは、中にどのような値が格納されるか想像しにくく感じます。 open_brackets 等、中に格納される値が想像しやすい変数名を付けるとよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、中身がわかる変数名を意識します

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.

3 participants