Skip to content

20. Valid Parentheses#7

Open
kazukiii wants to merge 2 commits intomainfrom
arai60/valid-parentheses
Open

20. Valid Parentheses#7
kazukiii wants to merge 2 commits intomainfrom
arai60/valid-parentheses

Conversation

@kazukiii
Copy link
Copy Markdown
Owner

問題へのリンク
https://leetcode.com/problems/valid-parentheses/description/

次に解く問題
206. Reverse Linked List

README.mdへ頭の中の言語化と記録をしています。

return False

open_bracket = stack.pop()
if not bracket_pairs[open_bracket] == c:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここは素直に

if bracket_pairs[open_bracket] != c:

で良いのではないでしょうか。

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.

奇妙な書き方をしていました。ご指摘ありがとうございます。

Comment on lines +10 to +13
if c in bracket_pairs:
stack.append(c)
else:
if not 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.

ここはネストを深くするよりcontinueで切った方が読みやすいなと感じます。

if c in bracket_pairs:
    stack.append(c)
    continue

if not stack:

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.

ありがとうございます。early continueしてみます。

@@ -0,0 +1,20 @@
class Solution:
def isValid(self, s: str) -> bool:
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.

何を入れるstackなのかわかる命名だと良いかもしれません。
brackets_stack とかですかね。

Comment on lines +4 to +8
bracket_pairs = {
'(': ')',
'{': '}',
'[': ']',
}
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_to_close
とか皆さんよく使われてます。

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 真似させていただきます。

Comment on lines +10 to +18
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

cだけ見るより、「stackのtopと合わせてペアができるか」に注目したほうが条件分岐が少なくなっていいのではと思いました。

(Pythonに詳しくなく、動くコードかは怪しいです...)

Suggested change
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
top = stack[-1]
is_match = False
match (top, c):
case ('(', ')'):
is_match = True
case ('{', '}'):
is_match = True
case ('[', ']'):
is_match = True
if is_match:
stack.pop()
else:
stack.append(c)

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.

コメントありがとうございます。
これは好みの問題ですが、stackに閉じカッコを入れる場合(上のコードで c in [')', '}', ']'] かつ is_match がfalse)は、その時点で全体としてのカッコ列の整合性が崩れるので return false すると頭の中のスタックが解放される感じがします。
例えば (}((((())))) とあったときに、3文字目以降は見なくていいよね、という感じです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@kazukiii
なるほどです。場合によっては早めにinvalid判定できるんですね

ありがとうございます〜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants