-
Notifications
You must be signed in to change notification settings - Fork 0
20. Valid Parentheses #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TORUS0818
wants to merge
1
commit into
main
Choose a base branch
from
20
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# Step1 | ||
|
||
かかった時間:32min | ||
|
||
計算量: | ||
sの長さをNとして | ||
時間計算量:O(N) 空間計算量:O(N) | ||
|
||
```python | ||
from collections import deque | ||
|
||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
parentheses_pairs = { | ||
'(': ')', | ||
'[': ']', | ||
'{': '}' | ||
} | ||
parentheses_stack = deque() | ||
for s_i in s: | ||
if s_i in ['(', '[', '{']: | ||
parentheses_stack.append(s_i) | ||
continue | ||
if not parentheses_stack: | ||
return False | ||
s_j = parentheses_stack.pop() | ||
if parentheses_pairs[s_j] != s_i: | ||
return False | ||
|
||
return False if parentheses_stack else True | ||
``` | ||
思考ログ: | ||
- まず問題文を読み間違える | ||
- ```([)]```こういうのもOKだと思って実装を始める、、 | ||
- 脱線から復帰(15分ほどロス、、) | ||
- 方針としては、 | ||
- 左括弧を貯めておくスタックを用意して、sを順にチェックしていく | ||
- 左括弧が来たらスタックに入れる | ||
- 右括弧が来たらスタックから取り出したものと比較する | ||
- そもそも取り出すものがなかったらアウト | ||
- 正しいペアでなかったらアウト | ||
- 正しいペアだったらさらにsの検査を進める | ||
- 最後、スタックに何か残ってたらアウト、そうでなければ合格 | ||
- あんまり思考の整理ができてない状態でコードを書いてる、、 | ||
- ”そもそも取り出すものがなかったらアウト”を忘れていて、モグラ叩きのようにデバッグしながら追加、、 | ||
- 命名も適当になってしまった、s_iとかs_j | ||
|
||
# Step2 | ||
|
||
講師役目線でのセルフツッコミポイント: | ||
- 変数名について(s_i, s_j) | ||
- dequeについて調べよう | ||
- https://docs.python.org/3/library/collections.html#collections.deque | ||
- https://github.com/python/cpython/blob/1b293b60067f6f4a95984d064ce0f6b6d34c1216/Modules/_collectionsmodule.c | ||
- 実装をチラ見 | ||
- linked-listで固定長配列が繋がったもの | ||
|
||
参考にした過去ログなど: | ||
- https://github.com/sakzk/leetcode/pull/6#discussion_r1595460679 | ||
- dictionary, dequeの調査など | ||
- https://github.com/sakupan102/arai60-practice/pull/7#discussion_r1584540368 | ||
- 変数命名の参考 | ||
- https://github.com/Mike0121/LeetCode/pull/2 | ||
- close-bracketがスタックに入った時点での早期終了について | ||
- https://github.com/tayzarnw/LeetCode/pull/7 | ||
- 変数命名の参考 | ||
- https://github.com/rm3as/code_interview/pull/4 | ||
- https://github.com/rossy0213/leetcode/pull/7 | ||
- https://github.com/sakupan102/arai60-practice/pull/7#pullrequestreview-1999411452 | ||
- プッシュダウンオートマトンの話 | ||
- https://github.com/cheeseNA/leetcode/pull/10#pullrequestreview-1965188602 | ||
- プッシュダウンオートマトン、チョムスキー階層の話がある | ||
- https://github.com/Kitaken0107/GrindEasy/pull/5 | ||
- https://github.com/colorbox/leetcode/pull/4#discussion_r1523522386 | ||
- チョムスキー階層、type-2、文脈自由文法、プッシュダウンオートマトン | ||
- - https://ja.wikipedia.org/wiki/%E3%83%81%E3%83%A7%E3%83%A0%E3%82%B9%E3%82%AD%E3%83%BC%E9%9A%8E%E5%B1%A4 | ||
- https://github.com/hayashi-ay/leetcode/pull/16 | ||
|
||
```python | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
brackets_stack = [] | ||
close_to_open = { | ||
')': '(', | ||
']': '[', | ||
'}': '{' | ||
} | ||
open_brackets = list(close_to_open.values()) | ||
for char in s: | ||
if char in open_brackets: | ||
brackets_stack.append(char) | ||
continue | ||
if not brackets_stack: | ||
return False | ||
if close_to_open[char] != brackets_stack.pop(-1): | ||
return False | ||
return not brackets_stack | ||
``` | ||
思考ログ: | ||
- まず変数名から直していく | ||
- ```parentheses_stack``` -> ```brackets_stack``` | ||
- ```parentheses_pairs``` -> ```close_to_open``` | ||
- ```s_i``` -> ```char``` | ||
- また、Step1の```s_j```のように無理に変数に入れなくても直接比較してしまった方がわかりやすい時もあるかも | ||
- データ構造について | ||
- https://github.com/sakzk/leetcode/pull/6#discussion_r1595460679 | ||
- 上記でも議論があったが、なぜ様々な選択肢から```deque```を選んだか?を考えること | ||
- 脳死で、stack->dequeのような判断は止める | ||
|
||
# Step3 | ||
|
||
かかった時間:3min | ||
|
||
```python | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
brackets_stack = list() | ||
close_to_open = dict((')(', '}{', '][')) | ||
for char in s: | ||
if char in close_to_open.values(): | ||
brackets_stack.append(char) | ||
continue | ||
if not brackets_stack: | ||
return False | ||
if close_to_open[char] != brackets_stack.pop(-1): | ||
return False | ||
return not brackets_stack | ||
``` | ||
思考ログ: | ||
- 普通に書いた方が分かりやすいと思うが、```dict((')(', '}{', ']['))```みたいな書き方もできる | ||
- Step2のように一旦```open_brackets```に入れた方が分かりやすい気もするが | ||
|
||
# Step4 | ||
|
||
```python | ||
``` | ||
思考ログ: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
リストのドキュメントを読んでいて気づきましたが、ここ
pop()
で大丈夫ですね。