Conversation
| subsets = [] | ||
| mask = (1 << len(nums)) - 1 | ||
| while mask >= 0: | ||
| subsets.append([num for i, num in enumerate(nums) if (mask >> i) & 1]) |
There was a problem hiding this comment.
0 かどうかを判定する際は、 implicit false を使用しないほうが良いかもしれません。
subsets.append([num for i, num in enumerate(nums) if (mask >> i) & 1 != 0])参考までにスタイルガイドへのリンクを共有いたします。
https://google.github.io/styleguide/pyguide.html#2144-decision
When handling integers, implicit false may involve more risk than benefit (i.e., accidentally handling None as 0). You may compare a value which is known to be an integer (and is not the result of len()) against the integer 0.
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。
There was a problem hiding this comment.
ビット演算でも implicit false を使用するべきではないのか分からないのですが、一応書き直しておきます。
頭に留めておきます。
| frontier = [([], -1)] | ||
|
|
||
| while frontier: | ||
| fixed, determined_idx = frontier.pop() |
There was a problem hiding this comment.
自分なら最初に 0 を入れ、 index という名前で受けると思いますが、趣味の範囲だと思います。
There was a problem hiding this comment.
他の方の回答にはそのパターンもありましたね。今回はこのままにしておきます。
https://leetcode.com/problems/subsets/description/