-
Notifications
You must be signed in to change notification settings - Fork 0
78. Subsets #51
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
potrue
wants to merge
1
commit into
main
Choose a base branch
from
78
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 #51
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,92 @@ | ||
| ## 何も見ずに解いてみる | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> subsets(vector<int>& nums) { | ||
| vector<vector<int>> result; | ||
| for (int subset = 0; subset < 1 << nums.size(); ++subset) { | ||
| vector<int> subset_in_vector; | ||
| for (int element = 0; element < nums.size(); ++element) { | ||
| if (subset & (1 << element)) { | ||
| subset_in_vector.emplace_back(nums[element]); | ||
| } | ||
| } | ||
| result.emplace_back(subset_in_vector); | ||
| } | ||
| return result; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## 他の人のコードを見てみる | ||
|
|
||
| https://github.com/tokuhirat/LeetCode/pull/51/files | ||
| https://github.com/ryosuketc/leetcode_arai60/pull/40 | ||
| https://github.com/fhiyo/leetcode/pull/51/files | ||
| https://github.com/Ryotaro25/leetcode_first60/pull/55/files | ||
|
|
||
| いろいろなやり方がありますね。 | ||
| 変数名のつけ方が難しいです。 | ||
|
|
||
| 今まで作ったsubsetsを広げていくやり方で書いてみたらこんな感じになりました。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> subsets(vector<int>& nums) { | ||
| vector<vector<int>> made_subsets = {{}}; | ||
| for (int num : nums) { | ||
| int current_size = made_subsets.size(); | ||
| for (int i = 0; i < current_size; ++i) { | ||
| vector<int> subset = made_subsets[i]; | ||
| subset.emplace_back(num); | ||
| made_subsets.emplace_back(subset); | ||
| } | ||
| } | ||
| return made_subsets; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| そういえば最近気づいたんですがleetcodeの実行環境だと日本語の変数名も使えるみたいですね。 | ||
| 下のコードどうでしょうか笑 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> subsets(vector<int>& 要素たち) { | ||
| vector<vector<int>> 今まで作った部分集合 = {{}}; | ||
| for (int 要素 : 要素たち) { | ||
| int 現時点のサイズ = 今まで作った部分集合.size(); | ||
| for (int インデックス = 0; インデックス < 現時点のサイズ; ++インデックス) { | ||
| vector<int> 部分集合 = 今まで作った部分集合[インデックス]; | ||
| 部分集合.emplace_back(要素); | ||
| 今まで作った部分集合.emplace_back(部分集合); | ||
| } | ||
| } | ||
| return 今まで作った部分集合; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## 最終コード | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> subsets(vector<int>& nums) { | ||
| vector<vector<int>> result; | ||
| for (int bits = 0; bits < 1 << nums.size(); ++bits) { | ||
| vector<int> subset; | ||
| for (int bit = 0; bit < nums.size(); ++bit) { | ||
| if (bits & (1 << bit)) { | ||
| subset.emplace_back(nums[bit]); | ||
| } | ||
| } | ||
| result.emplace_back(subset); | ||
| } | ||
| return result; | ||
| } | ||
| }; | ||
| ``` | ||
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.
変数名難しいところですが、bits という感じはあまりわかりませんでした。bit_mask はいかがでしょうか。
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.
step から変数名が洗練されている点はいいと思います。
bit_mask、私もいいんじゃないかなと思いました。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.
今でいうbits/bitをbit_mask/bitにするという感じでしょうか?
bits/bit_maskですかね。
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.
以下のような想定でした。
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.
個人的には
bitsだけでなくbit(というより1 << bit)の方もビットマスクであるという風に捉えられると思うのですが、少数派かもしれません。(なんとなくですが、マスクという名前からより範囲が狭い方を想像します)もっと意味的な情報を持たせた変数名を付けたいとすれば
bitmask_representing_subsetなどがよいかと思うのですが、長くなってしまうので難しいところですね・・・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.
前提として、コード全体を眺めるとbit全探索であることは伝わるはずなのでそこまでこだわらなくても良いかなとは思っています。個人的な感覚をお伝えすると、以下のような感じです。
nums: [1,2,3] としたとき、
各数字を使うかを 1 << nums.size() までの整数を用いることで組み合わせを表現する。111 = 7 で [1,2,3] が得られ、101 = 5 の場合 [1,3] が得られる。これが bit_mask に見えると思っています。
変数 bit は nums の各数字を走査するための変数なのでしょうがなく使っているという感じです (filter のような仕組みがあれば使わない変数、今回は 1 << bit が必要なため明示的に index を使わないといけないですが)。i でもよいぐらいの感覚です。
以下変なコードですが頭の中はこんなイメージでした。
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.
あ、いただいたコードでよくわかりました。
numsに対して操作を行うためのビットマスクということですね。
自分が思っていたのは、bitsに対して、1<<bitが(その要素を必要に応じて追加するという)操作を行うためのビットマスクになっているのではないかという感覚でした。
コードにおけるどの操作に注目するかによってどちらもビットマスクとして捉えられると思いますが、提案していただいた元のコードの(bits, bit)を(bit_mask, i)とする名付け方が1 << iでのビット操作感が薄れるという意味で総じてバランスいいように思います。
特に、bitをfor文で回している処理が他のところに(numsとbitsを引数として受け取るような)関数としてまとまっているような場合には、bitsはbit_maskという名前が適切だと思いました。