Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions 78/78.md
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数名難しいところですが、bits という感じはあまりわかりませんでした。bit_mask はいかがでしょうか。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

step から変数名が洗練されている点はいいと思います。bit_mask、私もいいんじゃないかなと思いました。

Copy link
Owner Author

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ですかね。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下のような想定でした。

        for (int bit_mask = 0; bit_mask < 1 << nums.size(); ++bit_mask) {
            vector<int> subset;
            for (int bit = 0; bit < nums.size(); ++bit) {
                if (bit_mask & (1 << bit)) {

Copy link
Owner Author

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などがよいかと思うのですが、長くなってしまうので難しいところですね・・・

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 でもよいぐらいの感覚です。
以下変なコードですが頭の中はこんなイメージでした。

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        n = len(nums)
        power_set = []
        for bit_mask in range(1 << n):
            bit_mask = format(bit_mask, f'0{n}b')
            subset = [num for num, mask in zip(nums, bit_mask) if mask != "0"]
            power_set.append(subset)
        return power_set

Copy link
Owner Author

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という名前が適切だと思いました。

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;
}
};
```