-
Notifications
You must be signed in to change notification settings - Fork 0
20. Valid Parentheses.md #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,117 @@ | ||
20. Valid Parentheses | ||
思考順に書いていく | ||
引用文は「」 | ||
|
||
step1 方針立たず不正解 | ||
```python | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
if s[0] == "(": | ||
for i in range(1,len(s)): | ||
if s[i] == ")": | ||
return True | ||
if s[0] == "{": | ||
for i in range(1,len(s)): | ||
if s[i] == "}": | ||
return True | ||
if s[0] == "[": | ||
for i in range(1,len(s)): | ||
if s[i] == "]": | ||
return True | ||
return False | ||
``` | ||
まあ不正解になるとは最初から感じていたが一応不正解のコードを書き残しておく。 | ||
stackは知っていたがどうやって、それを使って解くのかわからなかった。 | ||
また、pythonでスタックをどう実装するのかもわかっていなかった。 | ||
|
||
https://github.com/yus-yus/leetcode/pull/6 | ||
かなり勉強になった。 | ||
特に問題文にはないが、brackets以外が入力として与えられた場合も考えているのがすごくためになった。 | ||
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. これは凄く大切ですね。 |
||
以下のコードを参考に書くことにする。 | ||
また余力があればdequeの解法も真似する。 | ||
|
||
```Python | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
valid_input = ['(', ')', '{', '}', '[', ']'] | ||
open_to_close = {'(':')', '{':'}', '[':']'} | ||
stack = [] | ||
|
||
for char in s: | ||
# 括弧以外の入力が来た場合の処理 無視する | ||
if not char in valid_input: | ||
continue | ||
if char in open_to_close: | ||
stack.append(char) | ||
continue | ||
if not stack: | ||
return False | ||
if char != open_to_close[stack.pop()]: | ||
return False | ||
|
||
return not stack | ||
``` | ||
step2 | ||
```python | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
valied_input = ["(",")","{","}","[","]"] | ||
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. valid_input = "(){}[]" |
||
open_to_close = {"(":")","{":"}","[":"]"} | ||
stack = [] | ||
|
||
for char in s: | ||
#かっこ以外の入力が来た場合は無視する処理 | ||
if not char in valied_input: | ||
continue | ||
if char in open_to_close: | ||
stack.append(char) | ||
continue | ||
if not stack: | ||
return False | ||
if char != open_to_close[stack.pop()]: | ||
return False | ||
return not stack | ||
``` | ||
この解法が書きやすかったから、これでstep3いく。 | ||
|
||
かっこの英語名が気になったので調べた。米語違いすぎだろ | ||
() 米語はparentheses 英語はbrackets | ||
[] square brackets | ||
{} curly brackets | ||
<> angle brackets | ||
(https://www.rarejob.com/englishlab/column/20210928_02/) | ||
|
||
「valid parentheses は、チョムスキー階層、タイプ-2、文脈自由文法だから、プッシュダウンオートマトンで書ける、を多分連想します。」by(keita odaさん) | ||
(https://discord.com/channels/1084280443945353267/1201211204547383386/1202541275115425822) | ||
上の用語わからなすぎて調べる。 | ||
おそらく形式言語理論の概念を用いてこの問題を解釈している? | ||
うちの学校のカリキュラムにはないが、形式言語理論の勉強はSWEとして必須? | ||
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. そうだったんですね。余力があれば勉強してみます! |
||
|
||
step3 | ||
1回目4分41 | ||
2回目3分38 | ||
3回目2分48 | ||
|
||
```python | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
valid_input = ["(",")","{","}","[","]"] | ||
open_to_close = {"(":")","{":"}","[":"]"} | ||
stack = [] | ||
|
||
for char in s: | ||
#かっこ以外がきたら無視する処理 | ||
if not char in valid_input: | ||
continue | ||
if char in open_to_close: | ||
stack.append(char) | ||
continue | ||
if not stack: | ||
return False | ||
if char != open_to_close[stack.pop()]: | ||
return False | ||
return not 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.
コードの書き忘れでしょうか?
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.
まだ完成していないので終わり次第レビュー依頼に投げさせていただきます。
その際はよろしくおねがいいたします
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.
失礼しました!!