Conversation
| - ([)]のような事例に対応できていない。 | ||
|
|
||
| ```python | ||
| class Solution: |
There was a problem hiding this comment.
カッコが登場する順番も考慮する必要があるので、カウントするだけではだめですね。コードを書く前に手作業でやるならどうすれば良いのかということを考えると良いと思います。
There was a problem hiding this comment.
ありがとうございます。手作業でやるならどうするかを考えたことはなかったので次から試してみたいと思います。
| collections.dequeを用いてみる | ||
|
|
||
| - [Listとdequeの計算量の違い](https://wiki.python.org/moin/TimeComplexity) | ||
| - append,popどちらもO(1)なので今回は差がない |
There was a problem hiding this comment.
定数倍の処理を無視していて、少し乱暴に思えます。dequeの方は双方向LinkedListを使っているため、単にStackとして使用するならListに比べてオーバーヘッドがあると思います。
| ``` | ||
|
|
||
| - stackに入れて後ろから確かめていくのは思いつかなかった | ||
| - `return not stack`で返すとbool値になるのは知らなかった |
There was a problem hiding this comment.
| return not stack | ||
| ``` | ||
|
|
||
| いろいろ調べていたら4時間くらいかけてしまった。これで良いのだろうか? |
There was a problem hiding this comment.
LeetCode解くことがゴールではないのでいいのではないでしょうか。慣れれば調べるのに掛かる時間も減ると思います。
| if c in open_to_close: | ||
| stack.append(c) | ||
| continue | ||
| if not stack or c != open_to_close[stack.pop()]: |
There was a problem hiding this comment.
自分はifの条件を分けて書いてしまいますね。あとstackの操作もif分とは別にやると思います。まあ好みかもしれないです。
| if not stack or c != open_to_close[stack.pop()]: | ||
| return False | ||
|
|
||
| return not stack |
There was a problem hiding this comment.
ここの表記を変えるとどのようなニュアンスの違いがでるのかお聞きしたいです。それとも個人の好みに過ぎないのでしょうか?
There was a problem hiding this comment.
個人的には好みの範囲かなと思ってます。どの記法に慣れているかだったりPython以外の馴染みのある言語でも読みやすさも変わってきそうですし。個人的にはlen(stack) == 0の方が配列の要素が0であることを確認しているのが直感的に分かるのでlenを使う方が好みです。
大事なのは、色々書き方があることを知っているうえで自分が気に入っているのを使うというプロセスかなと思います。
There was a problem hiding this comment.
なるほど、ありがとうございます。
好みを決めれるほど記法を知らないのでコメントいただけて助かります。
There was a problem hiding this comment.
PEP-8 と Google Style Guide は strings, lists, tuples は implicit でということでしたね。
趣味の範囲でしょう。大規模開発などでは周りを見て合わせましょう。
https://google.github.io/styleguide/pyguide.html#2144-decision
https://peps.python.org/pep-0008/#other-recommendations:~:text=For%20sequences%2C%20(strings%2C%20lists%2C%20tuples)%2C%20use%20the%20fact%20that%20empty%20sequences%20are%20false%3A
| collections.dequeを用いてみる | ||
|
|
||
| - [Listとdequeの計算量の違い](https://wiki.python.org/moin/TimeComplexity) | ||
| - append,popどちらもO(1)なので今回は差がない |
There was a problem hiding this comment.
計算量に興味がありすぎるかもしれません。計算量は計算時間を見積もるための手段です。だから、定数倍にも興味を持ちましょう。また、内部実装とできることの差異により注目しましょう。
| return not stack | ||
| ``` | ||
|
|
||
| いろいろ調べていたら4時間くらいかけてしまった。これで良いのだろうか? |
There was a problem hiding this comment.
問題にもよりますが、この問題からは相当たくさんのことを連想することが分かったと思います。はじめはそれでいいのです。
| "{": "}", | ||
| "[": "]" | ||
| } | ||
| open_brackets = LifoQueue() |
There was a problem hiding this comment.
queue.LifoQueue() は内部でスレッド同期が取られるため、取られないデータ構造に比べて動作が遅いと思います。プロデューサー・コンシューマーパターンを実装するときなど特別な場合を除き、避けたほうがよいと思います。
|
|
||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| open_to_close = { |
There was a problem hiding this comment.
こちらの対応の取り方のほうが、括弧の対応と一致しているため、個人的には好きです。
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
| pair = {')':'(','}':'{',']':'['} |
There was a problem hiding this comment.
下の回答で修正されていましたが、ここ目がチカチカするので改行か、せめて : と , の後に空白を入れるといいと思います
There was a problem hiding this comment.
承知しました。今度からは気をつけたいと思います。
問題文
https://leetcode.com/problems/valid-parentheses/description/