Open
Conversation
844. Backspace String Compare https://leetcode.com/problems/backspace-string-compare/
nodchip
reviewed
May 26, 2024
| ```python | ||
| class Solution: | ||
| def backspaceCompare(self, s: str, t: str) -> bool: | ||
| s_stack = [] |
| t_stack.pop() | ||
|
|
||
|
|
||
| if len(s_stack) != len(t_stack): |
There was a problem hiding this comment.
list の == は内容まで比較しますので、 return s_stack == t_stack でよいと思います。
Owner
Author
There was a problem hiding this comment.
こちら知らなかったです。発見でした。
ありがとうございます。
| loop_cnt -= 1 | ||
|
|
||
| return True | ||
|
|
oda
reviewed
May 27, 2024
| t_stack = [] | ||
|
|
||
| for char in s: | ||
| if char != '#': |
There was a problem hiding this comment.
char は言語によっては予約語なので、私は避けますが趣味の問題です。
同じ処理は二度書かずに関数にしましょう。
if 文字が '#' でないとき:
elif : # すなわち '#' なとき
と二重否定にしているのひねくれていませんか。
if char == '#':
if s_stack:
s_stack.pop()
continue
s_stack.append(char)
のほうが素直でしょう。
Owner
Author
There was a problem hiding this comment.
たしかに自分のコード、二重否定なのと無駄に否定系使ってるのが読みづらかったです。
頂いたコード読みやすかったです。
continueいまだに使い慣れてないので、論理の構造の幅のところを見直してみます。
oda
reviewed
May 27, 2024
| t_stack.pop() | ||
|
|
||
|
|
||
| if len(s_stack) != len(t_stack): |
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/backspace-string-compare/