Conversation
| elif "{}" in s: | ||
| s = s.replace("{}", "") | ||
|
|
||
| if s: return True |
There was a problem hiding this comment.
if s: return False
else: return Trueが正しいように思います。
また、 Implicit False を用いて、
return not sと書いたほうがシンプルだと思います。
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
| closeToOpen = {")": "(", "]": "[", "}": "{"} |
There was a problem hiding this comment.
以下コメントをご参照ください。
tNita/arai60#3 (comment)
There was a problem hiding this comment.
snake_case意識します!
ありがとうございます
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
| closeToOpen = {")": "(", "]": "[", "}": "{"} |
There was a problem hiding this comment.
open_to_close のほうが、見た目の違和感が少ないように感じます。ほかの方でこのように書かれている方がいらっしゃいます。ぜひ参考にしてみてください。
| ## 別解 | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] |
There was a problem hiding this comment.
個人的には変数名に、どのようなデータ構造として使うかより、中にどのような値が含まれているかを付けたほうが、読み手に取って理解の助けになると思います。 open_brackets はいかがでしょうか?
There was a problem hiding this comment.
毎回こういった変数に名前をつけるのが苦手で良い命名が思いつかないです。。。
参考になります
There was a problem hiding this comment.
個人的には変数名に、どのようなデータ構造として使うかより、中にどのような値が含まれているかを付けたほうが、読み手に取って理解の助けになる
私も同意です。
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| while s: | ||
| if "()" in s: |
There was a problem hiding this comment.
競技プログラミング同好会の hmuta 氏が、このようなコードを書かれていたのを思い出しました。
There was a problem hiding this comment.
これ、チェックせずにどんどん replace し、短くならなくなったら False という方法はできることはできます。
計算量は2乗ですが、Native コードなのでそんなに遅くない気がします。
TakayaShirai
left a comment
There was a problem hiding this comment.
レビュー後に回答の修正をする場合は、前のファイルを修正するのではなく、step4.py などのファイルを追加する方式をおすすめします!修正後にレビューをした場合に、過去のコメントとコードが一致しておらずわかりにくくなる場合があります!
| while s: | ||
| if "()" in s: | ||
| s = s.replace("()", "") | ||
| elif "[]" in s: |
| open_to_close = {"()": ")", "[]": "]", "{": "}"} | ||
|
|
There was a problem hiding this comment.
前回のコミット(baadf67) で、open_to_closeに余計な括弧が入り込んでしまっているみたいです。
また、close_to_open から open_to_close にする場合は以下の変更を加える必要があると思います。
if c in open_to_closeの方で stack に追加elseの方で検証を行う必要。
これは、valid な出力の場合 open → close の順に括弧がくるためです。具体的には、if c in open_to_closeで検証を行う方法だと、入力が ()の場合、最初に(がきますが、検証を行う際に close_brackets の中身がまだ空なため False になります。
たとえば、以下のような書き方があると思います。
class Solution:
def isValid(self, s: str) -> bool:
close_bracktes = []
open_to_close = {"(": ")", "[": "]", "{": "}"}
for c in s:
if c in open_to_close:
close_bracktes.append(open_to_close[c])
else:
if not close_bracktes:
return False
expected_bracket = close_bracktes.pop()
if c != expected_bracket:
return False
return not close_bracktes| ## 別解 | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] |
There was a problem hiding this comment.
個人的には変数名に、どのようなデータ構造として使うかより、中にどのような値が含まれているかを付けたほうが、読み手に取って理解の助けになる
私も同意です。
問題リンク
https://leetcode.com/problems/valid-parentheses/
問題文の概要
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
次に解く予定の問題
https://leetcode.com/problems/reverse-linked-list/description/