Skip to content

Conversation

@potrue
Copy link
Owner

@potrue potrue commented Aug 19, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants