-
Notifications
You must be signed in to change notification settings - Fork 0
20. Valid Parentheses #7
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## 考察 | ||
- 過去に解いたことあり | ||
- 方針 | ||
- stackを使って対応を見ていく | ||
- 前から文字を見ていって、open bracketが来たらstackに積む | ||
- close bracketが来たら、stackから要素をpopして対応をチェックする(stackが空じゃないかだけ注意) | ||
- 最後にstackが空になっていたらOK | ||
- time: O(n), space: O(n) | ||
- あとは実装 | ||
|
||
## Step1 | ||
- 上のアルゴリズムを実装 | ||
- time: O(n), space: O(n) | ||
|
||
## Step2 | ||
- 手続き的にbracketの対応関係を判定していた箇所を、辞書を使って宣言的な記述に変更 | ||
- 結果、余計なif文がかなり減った | ||
|
||
## Step3 | ||
- 1回目: 1m51s | ||
- 2回目: 1m53s | ||
- 3回目: 1m28s | ||
|
||
## Step4 | ||
- レビューを元にコードを修正 | ||
- 変数名の見直し | ||
- stack -> bracket_stack | ||
- 何を入れるstackなのかを明確に | ||
- bracket_pairs -> open_to_close | ||
- 例えば、open_to_close[open_bracket] という記述を見たときにclose_bracketが返ってくることが一目でわかる | ||
- より具体的になって、わかりやすくなった感覚 | ||
- `if not open_to_close[open_bracket] == c:` と書いていたところを `if open_to_close[open_bracket] != c:`に | ||
- early continueしてネストを浅く |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] | ||
for c in s: | ||
if c in ['(', '{', '[']: | ||
stack.append(c) | ||
else: | ||
if not stack: | ||
return False | ||
|
||
open_bracket = stack.pop() | ||
if c == ')': | ||
if not open_bracket == '(': | ||
return False | ||
elif c == '}': | ||
if not open_bracket == '{': | ||
return False | ||
else: | ||
if not open_bracket == '[': | ||
return False | ||
|
||
return not stack |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] | ||
bracket_pairs = { | ||
'(': ')', | ||
'{': '}', | ||
'[': ']', | ||
} | ||
Comment on lines
+4
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. いい名前ですね。 |
||
for c in s: | ||
if c in bracket_pairs: | ||
stack.append(c) | ||
else: | ||
if not stack: | ||
Comment on lines
+10
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここはネストを深くするよりcontinueで切った方が読みやすいなと感じます。 if c in bracket_pairs:
stack.append(c)
continue
if not stack: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。early continueしてみます。 |
||
return False | ||
|
||
open_bracket = stack.pop() | ||
if not bracket_pairs[open_bracket] == c: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここは素直に if bracket_pairs[open_bracket] != c: で良いのではないでしょうか。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 奇妙な書き方をしていました。ご指摘ありがとうございます。 |
||
return False | ||
|
||
return not stack |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||||
def isValid(self, s: str) -> bool: | ||||||||||||||||||||||||||||||||||||||||||||||||||
stack = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||
bracket_pairs = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
'(': ')', | ||||||||||||||||||||||||||||||||||||||||||||||||||
'{': '}', | ||||||||||||||||||||||||||||||||||||||||||||||||||
'[': ']', | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
for c in s: | ||||||||||||||||||||||||||||||||||||||||||||||||||
if c in bracket_pairs: | ||||||||||||||||||||||||||||||||||||||||||||||||||
stack.append(c) | ||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
if not stack: | ||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
open_bracket = stack.pop() | ||||||||||||||||||||||||||||||||||||||||||||||||||
if not bracket_pairs[open_bracket] == c: | ||||||||||||||||||||||||||||||||||||||||||||||||||
return False | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+10
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cだけ見るより、「stackのtopと合わせてペアができるか」に注目したほうが条件分岐が少なくなっていいのではと思いました。 (Pythonに詳しくなく、動くコードかは怪しいです...)
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kazukiii ありがとうございます〜 |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return not stack |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
bracket_stack = [] | ||
open_to_close = { | ||
'(': ')', | ||
'{': '}', | ||
'[': ']', | ||
} | ||
for c in s: | ||
if c in open_to_close: | ||
bracket_stack.append(c) | ||
continue | ||
|
||
if not bracket_stack: | ||
return False | ||
|
||
open_bracket = bracket_stack.pop() | ||
if open_to_close[open_bracket] != c: | ||
return False | ||
|
||
return not bracket_stack |
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.
何を入れるstackなのかわかる命名だと良いかもしれません。
brackets_stack
とかですかね。