-
Notifications
You must be signed in to change notification settings - Fork 0
Create 392. Is Subsequence.md #57
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
Open
tokuhirat
wants to merge
1
commit into
main
Choose a base branch
from
392.-Is-Subsequence
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # 392. Is Subsequence | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - 手作業でやることを実装した。 | ||
| - while-else を使うことでコードは短くなるが、読みやすいかは微妙なところ。 | ||
| - i, j が s, t に対応するかはなんとも言えないが、短いコードなので可とする。 | ||
| ```python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| i = 0 | ||
| j = 0 | ||
| while i < len(s): | ||
| while j < len(t): | ||
| if s[i] == t[j]: | ||
| i += 1 | ||
| j += 1 | ||
| break | ||
| j += 1 | ||
| else: | ||
| return False | ||
| return True | ||
| ``` | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://github.com/hayashi-ay/leetcode/pull/64/files | ||
| - STEP1 と同じ作業をとてもわかりやすく書いている。2変数あると while のネストで書こうとしてしまうのがよくない。 | ||
| - 終了条件として、STEP1 のコードでは、s の末尾に到達すると break を経由して外の while を抜けるので True で終了、t の末尾まで到達して else に入ると False で終了。 | ||
| - 結局これは while 文をまとめて書いて、s の末尾まで到達したかどうかで判定可能。こうすると以下のコードになる。 | ||
| ```python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| i = 0 | ||
| j = 0 | ||
| while i < len(s) and j < len(t): | ||
| if s[i] == t[j]: | ||
| i += 1 | ||
| j += 1 | ||
| else: | ||
| j += 1 | ||
| return i == len(s) | ||
| ``` | ||
| - 変形として while True で書くこともできる。 | ||
| ```python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| i = 0 | ||
| j = 0 | ||
| while True: | ||
| if i == len(s): | ||
| return True | ||
| if j == len(t): | ||
| return False | ||
| if s[i] == t[j]: | ||
| i += 1 | ||
| j += 1 | ||
| else: | ||
| j += 1 | ||
| ``` | ||
| - for 文を使うことも可 | ||
| ```python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| if not s: | ||
| return True | ||
| i = 0 | ||
| for character in t: | ||
| if s[i] == character: | ||
| i += 1 | ||
| if i == len(s): | ||
| return True | ||
| return False | ||
| ``` | ||
| - https://discord.com/channels/1084280443945353267/1225849404037009609/1243290893671465080 | ||
| - 変形たくさんある。 | ||
|
|
||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| 添字一つの方がわかりやすいと思った。 | ||
| ```python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| if not s: | ||
| return True | ||
| i = 0 | ||
| for character in t: | ||
| if s[i] == character: | ||
| i += 1 | ||
| if i == len(s): | ||
| return True | ||
| return False | ||
| ``` | ||
|
|
||
| 2分,1分,1分で3回Accept | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
自分もこの形が良いかなと思いました。
2つのものを比較するときに、両方とも走らせるのは「工夫しているなぁ」と感じるので、このくらいの複雑さならこれで良いと思います