Merged
Conversation
nodchip
reviewed
Apr 30, 2024
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
| pairs = {')': '(', '}': '{', ']': '['} |
There was a problem hiding this comment.
pairs ですと何のペアか分かりづらいかもしれません。 close_to_opens はいかがでしょうか?
Owner
Author
There was a problem hiding this comment.
よく考えたらたしかにそうですね。
ありがとうございます!
| pairs = {')': '(', '}': '{', ']': '['} | ||
| closes = [')', '}', ']'] | ||
| for c in s: | ||
| if c in closes: |
| closes = [')', '}', ']'] | ||
| for c in s: | ||
| if c in closes: | ||
| if len(stack) == 0 or stack.pop() != pairs[c]: |
There was a problem hiding this comment.
if not stack or stack.pop() != pairs[c]: としてはいかがでしょうか?
Owner
Author
There was a problem hiding this comment.
個人的に、not stackよりlen(stack) == 0のほうが何をチェックしているのかわかりやすく表現できているのかなと考えました。
There was a problem hiding this comment.
len(stack) == 0 でも大丈夫だと思います。チーム内でよく使われるほうの記法を使うことをお勧めいたします。
liquo-rice
reviewed
Apr 30, 2024
| """ | ||
| 3m51s | ||
|
|
||
| 2回くらい変な凡ミスでエラー吐いてしまった |
Owner
Author
There was a problem hiding this comment.
最後をlen(stack)のままにしてしまったり
else:
stack.append(stack)
にしてしまったりですね
There was a problem hiding this comment.
なるほど、ただのタイポだとは思いますが、実行前に目視チェックで気づけるといいですね。
Owner
Author
There was a problem hiding this comment.
そうですね~
書ききったときにあまりチェックせずに出してしまうことがあって、目視のチェックをしっかりやる習慣をつけないとなと思っています。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題:https://leetcode.com/problems/valid-parentheses/description/
参考:
https://github.com/SuperHotDogCat/coding-interview/pull/6/files
https://github.com/nittoco/leetcode/pull/5/files
https://github.com/sakupan102/arai60-practice/pull/7/files
https://github.com/Mike0121/LeetCode/pull/2/files