-
Notifications
You must be signed in to change notification settings - Fork 0
Decode Ways #147
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
tom4649
wants to merge
1
commit into
main
Choose a base branch
from
91.Decode-Ways
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
Decode Ways #147
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,38 @@ | ||
| # 91. Decode Ways | ||
|
|
||
| ## step1 | ||
| DPで書いた。8mほど。配るDP。やや変数名を修正。 | ||
|
|
||
| ## step2 | ||
| もらうDPで書いた。この場合は2変数で管理できる。 | ||
|
|
||
| LeetCodeや他の方のSolutionを見ても新しいものはないので、LLMに聞いてみるとメモ化再帰を提案された。 | ||
|
|
||
| トップダウンの解法 | ||
|
|
||
| ```python | ||
| from functools import cache | ||
|
|
||
| class Solution: | ||
| def numDecodings(self, s: str) -> int: | ||
|
|
||
| @cache | ||
| def dfs(i: int) -> int: | ||
| # 文字列の最後まで到達できたら、1つの有効な分割方法が見つかったということ | ||
| if i == len(s): | ||
| return 1 | ||
| # 0から始まるデコードは存在しない | ||
| if s[i] == "0": | ||
| return 0 | ||
|
|
||
| # パターン1: 1文字としてデコードする | ||
| ans = dfs(i + 1) | ||
|
|
||
| # パターン2: 2文字としてデコードする | ||
| if i + 1 < len(s) and (s[i] == "1" or (s[i] == "2" and s[i+1] <= "6")): | ||
| ans += dfs(i + 2) | ||
|
|
||
| return ans | ||
|
|
||
| return dfs(0) | ||
| ``` | ||
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,23 @@ | ||
| class Solution: | ||
| def numDecodings(self, s: str) -> int: | ||
| if not s or s[0] == "0": | ||
| return 0 | ||
|
|
||
| dp = [0] * (len(s) + 1) | ||
| dp[0] = 1 | ||
|
|
||
| for i in range(len(s)): | ||
| if s[i] == "0": | ||
| continue | ||
| dp[i+1] += dp[i] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのコメントをご参照ください。 |
||
| if i + 1 < len(s) and int(s[i:i+2]) <= 26: | ||
| dp[i+2] += dp[i] | ||
|
|
||
| return dp[-1] | ||
|
|
||
|
|
||
| # solution = Solution() | ||
| # print(solution.numDecodings("12")) | ||
| # print(solution.numDecodings("226")) | ||
| # print(solution.numDecodings("0")) | ||
| # print(solution.numDecodings("1001")) | ||
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,23 @@ | ||
| class Solution: | ||
| def numDecodings(self, s: str) -> int: | ||
| if not s or s[0] == "0": | ||
| return 0 | ||
|
|
||
| num_ways = [0] * (len(s) + 1) | ||
| num_ways[0] = 1 | ||
|
|
||
| for i in range(len(s)): | ||
| if s[i] == "0": | ||
| continue | ||
| num_ways[i+1] += num_ways[i] | ||
| if i + 1 < len(s) and int(s[i:i+2]) <= 26: | ||
| num_ways[i+2] += num_ways[i] | ||
|
|
||
| return num_ways[-1] | ||
|
|
||
|
|
||
| # solution = Solution() | ||
| # print(solution.numDecodings("12")) | ||
| # print(solution.numDecodings("226")) | ||
| # print(solution.numDecodings("0")) | ||
| # print(solution.numDecodings("1001")) |
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,25 @@ | ||
| class Solution: | ||
| def numDecodings(self, s: str) -> int: | ||
| if not s or s[0] == "0": | ||
| return 0 | ||
|
|
||
| num_end_with_i_minus_2 = 1 # 空文字列 | ||
| num_end_with_i_minus_1 = 1 # 最初の1文字 | ||
|
|
||
| for i in range(1, len(s)): | ||
| num_end_with_i = 0 | ||
|
|
||
| if s[i] != "0": | ||
| num_end_with_i += num_end_with_i_minus_1 | ||
|
|
||
| if s[i-1] != "0" and 10 <= int(s[i-1:i+1]) <= 26: | ||
| num_end_with_i += num_end_with_i_minus_2 | ||
|
|
||
| if num_end_with_i == 0: | ||
| return 0 | ||
|
|
||
| num_end_with_i_minus_2 = num_end_with_i_minus_1 | ||
| num_end_with_i_minus_1 = num_end_with_i | ||
|
|
||
| return num_end_with_i_minus_1 | ||
|
|
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.
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.
ans は、設問に対する解答というニュアンスを感じます。このプログラムの世界の外側の世界、 LeetCode の概念が混ざっているように感じられて、違和感を感じます。このプログラムの世界観の中で、この変数の中に格納される値を、端的な英単語・英語句で表した変数名を付けたほうが良いように思います。 num_ways はいかがでしょうか?