-
Notifications
You must be signed in to change notification settings - Fork 0
78. Subsets #48
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
2
commits into
main
Choose a base branch
from
78.Subsets
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
78. Subsets #48
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,57 @@ | ||
| # 78. Subsets | ||
|
|
||
| https://leetcode.com/problems/subsets/description/ | ||
|
|
||
| sol1.py: 何も考えないとbitmaskを思いついた。 | ||
| - 集合と聞いて | ||
| - 計算量 O(N*2^{N}) | ||
|
|
||
| sol2.py: DFSで解く | ||
| - 計算量 O(2^{N}) | ||
|
|
||
| sol3.py: backtrackを用いる | ||
| - 46. Permutationsで学んだ | ||
| - こちらを最初に自然に思いつくようにしたい | ||
|
|
||
| ## コメント集 | ||
| https://discord.com/channels/1084280443945353267/1200089668901937312/1222078986759311410 | ||
|
|
||
| > 理想的な話をすると、関数は中を見なくても戻って来る物の想像がつくのが理想です。 | ||
|
|
||
| 自分は関数名や変数名が分かりづらいとの指摘が多いのでこころがけたい | ||
|
|
||
| ## 他の方のコード | ||
|
|
||
| https://github.com/naoto-iwase/leetcode/pull/52/changes | ||
|
|
||
| https://github.com/mamo3gr/arai60/blob/78_subsets/78_subsets/memo.md | ||
|
|
||
| sol4.py: こんなに簡単に書けるのか | ||
|
|
||
| sol5.py | ||
| 勉強になったが、自分で書くのは無理だと判断 | ||
|
|
||
| mapを用いるときに、関数定義のデフォルト引数が関数の定義時に定義されることを利用している | ||
|
|
||
|
|
||
|
|
||
|
|
||
| 以下は関係ない話だが、思い出した話なので調べた | ||
|
|
||
| 上のvalueの渡し方は、評価戦略というよりかはデフォルト引数がいつ評価されるのかを決める話なので、少し違う話だった | ||
|
|
||
|
|
||
| https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions | ||
| 「デフォルト引数値は関数定義が実行されるときに左から右へ評価されます。 これは、デフォルト引数の式は関数が定義されるときにただ一度だけ評価され、同じ "計算済みの" 値が呼び出しのたびに使用されることを意味します。」 | ||
|
|
||
| =============================== | ||
|
|
||
|
|
||
|
|
||
| pythonは「参照の値渡し」call-by-object: オブジェクトへの参照を、値としてコピーして渡す | ||
|
|
||
| 値呼び call by value: 関数に渡す前に実引数の式を一度だけ評価し、その結果の値(のコピー)を仮引数に束縛 | ||
|
|
||
| 名前呼び call by name: 実引数は すぐには評価しないで、関数の本体でその名前が使われるたびに、元の式をその都度評価 | ||
|
|
||
| https://ja.wikipedia.org/wiki/%E8%A9%95%E4%BE%A1%E6%88%A6%E7%95%A5#:~:text=%E5%80%A4%E5%91%BC%E3%81%B3%EF%BC%88call%20by%20value%E3%80%81%E5%80%A4%E6%B8%A1%E3%81%97:%20pass%20by%20value%EF%BC%89%E3%81%AF%E3%80%81%E5%A4%9A%E3%81%8F%E3%81%AE%E8%A8%80%E8%AA%9E%E3%81%A7%E6%8E%A1%E7%94%A8%E3%81%95%E3%82%8C%E3%81%A6%E3%81%84%E3%82%8B%E5%85%B8%E5%9E%8B%E7%9A%84%E3%81%AA%E8%A9%95%E4%BE%A1%E6%88%A6%E7%95%A5%E3%81%A7%E3%81%82%E3%82%8B%E3%80%82%E5%80%A4%E5%91%BC%E3%81%B3%E3%81%A7%E3%81%AF%E3%80%81%E9%96%A2%E6%95%B0%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%97%E3%81%AB%E3%81%82%E3%82%8B%E5%AE%9F%E5%BC%95%E6%95%B0%E3%82%92%E8%A9%95%E4%BE%A1%E3%81%97%E3%80%81%E9%96%A2%E6%95%B0%E3%81%AE%E4%BB%AE%E5%BC%95%E6%95%B0%E3%82%92%E6%96%B0%E3%81%97%E3%81%84%E5%A4%89%E6%95%B0%E3%81%A8%E3%81%97%E3%81%A6%E3%81%9D%E3%81%AE%E5%80%A4%E3%81%AB%E6%9D%9F%E7%B8%9B%E3%81%97%E3%80%81%E3%81%97%E3%81%8B%E3%82%8B%E5%BE%8C%E3%81%AB%E9%96%A2%E6%95%B0%E6%9C%AC%E4%BD%93%E3%82%92%E5%AE%9F%E8%A1%8C%E3%81%99%E3%82%8B%E3%80%82%E9%96%A2%E6%95%B0%E3%81%AE%E4%B8%AD%E3%81%A7%E4%BB%AE%E5%BC%95%E6%95%B0%E3%81%A7%E3%81%82%E3%82%8B%E5%A4%89%E6%95%B0%E3%81%AB%E5%80%A4%E3%82%92%E4%BB%A3%E5%85%A5%E3%81%97%E3%81%A6%E3%82%82%E3%80%81%E3%81%9D%E3%82%8C%E3%81%AF%E5%B1%80%E6%89%80%E7%9A%84%E3%81%AA%E3%82%B3%E3%83%94%E3%83%BC%E3%81%B8%E3%81%AE%E4%BB%A3%E5%85%A5%E3%81%A7%E3%81%82%E3%82%8A%E3%80%81%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%97%E3%81%9F%E5%81%B4%E3%81%AE%E5%A4%89%E6%95%B0%E3%81%AB%E3%81%AF%E5%BD%B1%E9%9F%BF&text=%E5%90%8D%E5%89%8D%E5%91%BC%E3%81%B3%EF%BC%88call%20by%20name%E3%80%81%E5%90%8D%E5%89%8D%E6%B8%A1%E3%81%97:%20pass%20by%20name%EF%BC%89%E3%81%A7%E3%81%AF%E3%80%81%E9%96%A2%E6%95%B0%E3%81%AE%E5%BC%95%E6%95%B0%E3%81%AF%E5%85%A8%E3%81%8F%E8%A9%95%E4%BE%A1%E3%81%95%E3%82%8C%E3%81%9A%E3%80%81%E6%8D%95%E7%8D%B2%E5%9B%9E%E9%81%BF%E7%BD%AE%E6%8F%9B%EF%BC%88Capture%2DAvoiding%20Substitution%EF%BC%89%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E9%96%A2%E6%95%B0%E6%9C%AC%E4%BD%93%E5%86%85%E3%81%AB%E7%9B%B4%E6%8E%A5%E7%BD%AE%E6%8F%9B%E3%81%95%E3%82%8C%E3%82%8B%E3%80%82%E5%BC%95%E6%95%B0%E3%81%8C%E3%81%9D%E3%81%AE%E9%96%A2%E6%95%B0%E3%81%AE%E8%A9%95%E4%BE%A1%E3%81%A7%E4%BD%BF%E3%82%8F%E3%82%8C%E3%81%A6%E3%81%84%E3%81%AA%E3%81%84%E5%A0%B4%E5%90%88%E3%80%81%E3%81%9D%E3%81%AE%E5%BC%95%E6%95%B0%E3%81%AF%E5%85%A8%E3%81%8F%E8%A9%95%E4%BE%A1%E3%81%95%E3%82%8C%E3%81%AA%E3%81%84%E3%80%82%E5%BC%95%E6%95%B0%E3%81%8C%E8%A4%87%E6%95%B0%E5%9B%9E%E4%BD%BF%E3%82%8F%E3%82%8C%E3%81%A6%E3%81%84%E3%82%8B%E5%A0%B4%E5%90%88%E3%80%81%E3%81%9D%E3%81%AE%E5%BA%A6%E3%81%AB%E5%86%8D%E8%A9%95%E4%BE%A1%E3%81%95%E3%82%8C%E3%82%8B%E3%80%82%20%E5%80%A4%E5%91%BC%E3%81%B3%E3%81%AF%E3%81%9D%E3%81%AE%E5%BC%95%E6%95%B0%E3%81%8C%E5%85%A8%E3%81%8F%E4%BD%BF%E3%82%8F%E3%82%8C%E3%81%A6%E3%81%84%E3%81%AA%E3%81%8F%E3%81%A8%E3%82%82%E5%BF%85%E3%81%9A%E8%A9%95%E4%BE%A1%E3%81%99%E3%82%8B%E3%81%9F%E3%82%81%E3%80%81%E5%90%8D%E5%89%8D%E5%91%BC%E3%81%B3%E3%81%AE%E6%96%B9%E3%81%8C%E5%A5%BD%E3%81%BE%E3%81%97%E3%81%84%E3%81%A8%E3%81%99%E3%82%8B%E8%80%83%E3%81%88%E6%96%B9%E3%82%82%E3%81%82%E3%82%8B%E3%80%82 |
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,8 @@ | ||
| class Solution: | ||
| def subsets(self, nums: List[int]) -> List[List[int]]: | ||
| subsets = [] | ||
| mask = (1 << len(nums)) - 1 | ||
| while mask >= 0: | ||
| subsets.append([num for i, num in enumerate(nums) if (mask >> i) & 1]) | ||
| mask -= 1 | ||
| return subsets | ||
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,8 @@ | ||
| class Solution: | ||
| def subsets(self, nums: List[int]) -> List[List[int]]: | ||
| subsets = [] | ||
| mask = (1 << len(nums)) - 1 | ||
| while mask >= 0: | ||
| subsets.append([num for i, num in enumerate(nums) if (mask >> i) & 1 == 1]) | ||
| mask -= 1 | ||
| return subsets |
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,14 @@ | ||
| class Solution: | ||
| def subsets(self, nums: List[int]) -> List[List[int]]: | ||
| subsets = [] | ||
| frontier = [([], -1)] | ||
|
|
||
| while frontier: | ||
| fixed, determined_idx = frontier.pop() | ||
|
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. 自分なら最初に 0 を入れ、 index という名前で受けると思いますが、趣味の範囲だと思います。
Owner
Author
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 determined_idx == len(nums) - 1: | ||
| subsets.append(fixed) | ||
| continue | ||
| frontier.append((fixed, determined_idx + 1)) | ||
| frontier.append((fixed + [nums[determined_idx + 1]], determined_idx + 1)) | ||
|
|
||
| return subsets | ||
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,17 @@ | ||
| class Solution: | ||
| def subsets(self, nums: List[int]) -> List[List[int]]: | ||
| subsets = [] | ||
| subset = [] | ||
|
|
||
| def build_subset_after_index(index: int): | ||
| if index == len(nums): | ||
| subsets.append(subset.copy()) | ||
| return | ||
| subset.append(nums[index]) | ||
| build_subset_after_index(index + 1) | ||
| subset.pop() | ||
| build_subset_after_index(index + 1) | ||
|
|
||
| build_subset_after_index(0) | ||
|
|
||
| return subsets |
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,7 @@ | ||
| class Solution: | ||
| def subsets(self, nums: List[int]) -> List[List[int]]: | ||
| subsets = [[]] | ||
| for value in nums: | ||
| subsets.extend([subset + [value] for subset in subsets]) | ||
|
|
||
| return subsets |
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,10 @@ | ||
| import itertools | ||
|
|
||
|
|
||
| class Solution: | ||
| def subsets(self, nums: list[int]) -> list[list[int]]: | ||
| results = [[]] | ||
| for value in nums: | ||
| a, b = itertools.tee(results) | ||
| results = itertools.chain(a, map(lambda s, v=value: s + [v], b)) | ||
| return list(results) |
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.
0 かどうかを判定する際は、 implicit false を使用しないほうが良いかもしれません。
参考までにスタイルガイドへのリンクを共有いたします。
https://google.github.io/styleguide/pyguide.html#2144-decision
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。
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.
ビット演算でも implicit false を使用するべきではないのか分からないのですが、一応書き直しておきます。
頭に留めておきます。