File tree Expand file tree Collapse file tree 1 file changed +4
-2
lines changed Expand file tree Collapse file tree 1 file changed +4
-2
lines changed Original file line number Diff line number Diff line change @@ -258,9 +258,11 @@ a & mask == 0 说明 a 在第二位(从低到高)是 0
258258这就简单了,我们只需要将 1 左移 maxChoosableInteger + 1 位再减去 1 即可。一行代码搞定:
259259
260260``` py
261- picked == (1 << (maxChoosableInteger + 1 )) - 1
261+ picked == (1 << (maxChoosableInteger + 1 )) - 2
262262```
263263
264+ > 由于在这道题中,我们的 picked 最后一位永远是 0,因此这里是减 2 ,而不是 减 1。 具体参考这个 [ issue] ( https://github.com/azl397985856/leetcode/issues/577 )
265+
264266上面代码返回 true 表示满了, 否则没满。
265267
266268至此大家应该感受到了,使用位来代替 set 思路上没有任何区别。不同的仅仅是 API 而已。如果你只会使用 set 不会使用位运算进行状态压缩,只能说明你对位 的 api 不熟而已。多练习几道就行了,文末我列举了几道类似的题目,大家不要错过哦~
@@ -365,7 +367,7 @@ class Solution:
365367 def dp (picked , acc ):
366368 if acc >= desiredTotal:
367369 return False
368- if picked == (1 << (maxChoosableInteger + 1 )) - 1 :
370+ if picked == (1 << (maxChoosableInteger + 1 )) - 2 :
369371 return False
370372 for n in range (1 , maxChoosableInteger + 1 ):
371373 if picked & 1 << n == 0 :
You can’t perform that action at this time.
0 commit comments