-
Notifications
You must be signed in to change notification settings - Fork 0
Create 20. Valid Parentheses.md #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
mt2324
wants to merge
2
commits into
main
Choose a base branch
from
20.-Valid-Parentheses
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,96 @@ | ||
| ## STEP 1 | ||
| とりあえず書いてみた。 | ||
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| left = ['(', '{', '['] | ||
| stack = [] | ||
| for i in s: | ||
| if i in left: | ||
| stack.append(i) | ||
| continue | ||
| if stack == []: | ||
| return False | ||
| if i == ')': | ||
| if stack.pop() != '(': | ||
| return False | ||
| if i == '}': | ||
| if stack.pop() != '{': | ||
| return False | ||
| if i == ']': | ||
| if stack.pop() != '[': | ||
| return False | ||
| return stack == [] | ||
| ``` | ||
|
|
||
| ## STEP 2 | ||
| - 辞書で対応で明示すると意図が明確だし、コードの分岐が減る。 | ||
| - 鍵を開き括弧の方にすると辞書でシンプルに存在確認できてスマート | ||
| - pythonの配列の存在内の確認はimplicit falseが一般的らしい。https://google.github.io/styleguide/pyguide.html#2144-decision | ||
| - 直接returnで真偽値を書いた方が良い | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| open_to_close = {'(': ')', '{': '}', '[': ']'} | ||
| open_brackets = [] | ||
| for c in s: | ||
| if c in open_to_close: | ||
| open_brackets.append(c) | ||
| continue | ||
| if open_brackets and open_to_close[open_brackets.pop()] == c: | ||
| continue | ||
| return False | ||
| return not open_brackets | ||
| ``` | ||
|
|
||
| ## STEP 3 | ||
| 3回書いて練習。 | ||
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| open_to_close = {'(': ')', '{': '}', '[': ']'} | ||
| open_brackets = [] | ||
| for c in s: | ||
| if c in open_to_close: | ||
| open_brackets.append(c) | ||
| continue | ||
| if open_brackets and open_to_close[open_brackets.pop()] == c: | ||
| continue | ||
| return False | ||
| return not open_brackets | ||
| ``` | ||
| ネストが深くなるのはどうかなと思って、一つのif節にしましたが指摘を考慮して別のif節に分けるとこんな感じでしょうか。 | ||
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| open_to_close = {'(': ')', '{': '}', '[': ']'} | ||
| open_brackets = [] | ||
| for c in s: | ||
| if c in open_to_close: | ||
| open_brackets.append(c) | ||
| continue | ||
| if open_brackets: | ||
| latest_bracket = open_brackets.pop() | ||
| if open_to_close[latest_bracket] == c: | ||
| continue | ||
| return False | ||
| return not open_brackets | ||
| ``` | ||
| ネストを深くせずに分けるとするならばこう書いてもいいんですが、ネストが深くなってもcontinueとreturnが混ざっていない上の方がわかりやすい気がします。 | ||
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| open_to_close = {'(': ')', '{': '}', '[': ']'} | ||
| open_brackets = [] | ||
| for c in s: | ||
| if c in open_to_close: | ||
| open_brackets.append(c) | ||
| continue | ||
| if not open_brackets: | ||
| return False | ||
| if open_to_close[open_brackets[-1]] != c: | ||
| return False | ||
| open_brackets.pop() | ||
| return not open_brackets | ||
| ``` | ||
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.
二つ目を別のif節の中に入れたほうが好みです(副作用のあるpopして大丈夫なの?と不安になりました、結局その場合 continueを踏んでFalseを返すのだと、続きを読めばわかりますが)
別のif節でも、stack[-1]で先頭を確認してから pop するほうが素直かな、と
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がif節の中に現れるとちょっと不安ですね。ありがとうございます。