Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 競技プロ就活部PR用/20. Valid Parentheses 4th.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
時間: 3m51s<br>
Error: 1 (continue漏れ)<br>
時間計算量: O(N)<br>
空間計算量: O(N)<br>
N: sの長さ<br>

```python
class Solution:
def isValid(self, s: str) -> bool:
open_to_close_bracket = {"(": ")", "{": "}", "[": "]"}
unclosed_open_brackets = []

for bracket in s:
if bracket in open_to_close_bracket:
unclosed_open_brackets.append(bracket)
continue
if len(unclosed_open_brackets) == 0 or open_to_close_bracket[unclosed_open_brackets[-1]] != bracket:
return False
unclosed_open_brackets.pop()

return not unclosed_open_brackets
```
26 changes: 26 additions & 0 deletions 競技プロ就活部PR用/20. Valid Parentheses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

変数名に関して:
今回、stackはただのpopするためだけのものなので、stackでも良いかと思いました。
またdictに関しては何を保存しているのかがわからなくなるので、bracket_があった方が良いと思います。

```python
class Solution:
def isValid(self, s: str) -> bool:
bracket_dict = {"(": ")", "{": "}", "[": "]"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

dictなのは型からわかるので、不要な気がします。
open_to_close_bracketとかでどうでしょう?

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.

ありがとうございます。そうですね、open_to_close_bracketの方がわかりやすいです。
変数名に型を入れるのは、実際の開発でコードが長く型がわかりづらくなる場合につけると思いましたが、Leet Codeのような短いコードでは基本不要と考えて良いでしょうか?(変数名の長さに余裕があればつける程度)

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

Choose a reason for hiding this comment

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

承知しました。型を明示することにメリットがあるかを都度考えます。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LeetCodeのプラットフォーム上のエディタとかは別ですか、VSCodeとかでは定義や型情報のを見れると思うので実際の開発ではそこまでメリットはないかなと思います。

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.

ありがとうございます。確かにそうですね、頭に入れておきます。

bracket_stack = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

open/close bracketsのどっちが入っているかわかる名前がいいかと思います。unclosed_open_bracketsとかexpected_close_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.

ありがとうございます。
なるほど、確かに結局左側のカッコしか入らないですね。わかりやすいです。


for bracket in s:
if bracket in bracket_dict.keys():
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

未確認ですが、.keys()は不要かも?

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.

はい、.keys()は.items()と自分が混乱するので明示的に書くようにしてますが、最終的に不要ですね。

bracket_stack.append(bracket)
continue

if len(bracket_stack) == 0:
return False

if bracket_dict[bracket_stack[-1]] != bracket:
return False

bracket_stack.pop()

return not bracket_stack
```