Skip to content

Valid Parentheses#5

Merged
Exzrgs merged 3 commits intomainfrom
arai60-valid-parentheses
May 20, 2024
Merged

Valid Parentheses#5
Exzrgs merged 3 commits intomainfrom
arai60-valid-parentheses

Conversation

class Solution:
def isValid(self, s: str) -> bool:
stack = []
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.

pairs ですと何のペアか分かりづらいかもしれません。 close_to_opens はいかがでしょうか?

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.

よく考えたらたしかにそうですね。
ありがとうございます!

pairs = {')': '(', '}': '{', ']': '['}
closes = [')', '}', ']']
for c in s:
if c in closes:
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 c in pairs: としてはいかがでしょうか?

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.

たしかにそれでよいかもしれないです!

closes = [')', '}', ']']
for c in s:
if c in closes:
if len(stack) == 0 or stack.pop() != pairs[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 not stack or stack.pop() != pairs[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.

個人的に、not stackよりlen(stack) == 0のほうが何をチェックしているのかわかりやすく表現できているのかなと考えました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

len(stack) == 0 でも大丈夫だと思います。チーム内でよく使われるほうの記法を使うことをお勧めいたします。

"""
3m51s

2回くらい変な凡ミスでエラー吐いてしまった
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ちなみにどんなミスでしたか?

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.

最後をlen(stack)のままにしてしまったり

else:
    stack.append(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.

なるほど、ただのタイポだとは思いますが、実行前に目視チェックで気づけるといいですね。

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.

そうですね~
書ききったときにあまりチェックせずに出してしまうことがあって、目視のチェックをしっかりやる習慣をつけないとなと思っています。

@Exzrgs Exzrgs merged commit d709610 into main May 20, 2024
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