Skip to content

Commit fbf645c

Browse files
committed
subset binary
1 parent 3ca8294 commit fbf645c

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

leetcode/subset-binary.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# @Date : Jul-12-19 16:37
4+
# @Author : Your Name ([email protected])
5+
# @Link : http://example.org
6+
7+
import os
8+
9+
10+
def subset_binary(nums):
11+
"""
12+
子集二进制数
13+
最小的len(n)*0 都没有元素,空集
14+
最小的len(n)*1 都有元素,该集合本身
15+
0~2^n-1
16+
根据二进制数,取集合中都元素
17+
"""
18+
n = len(nums)
19+
ans = []
20+
for b in range(1 << n):
21+
subset = []
22+
i = 0
23+
while b != 0:
24+
print("b: %d" % b)
25+
if b & 0x01:
26+
# print("i: %d" % i)
27+
# print("b: %d" % b)
28+
subset.append(nums[i])
29+
i += 1
30+
b >>= 1
31+
ans.append(subset)
32+
return ans
33+
34+
35+
class Solution:
36+
def subsets(self, nums: list) -> list:
37+
return subset_binary(nums)
38+
39+
40+
def main():
41+
nums = [1, 2, 3, 4]
42+
nums = [1, 2, 3]
43+
ans = subset_binary(nums)
44+
print(ans)
45+
46+
47+
if __name__ == "__main__":
48+
main()

leetcode/subset-dp.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,35 @@
55
# @Link : https://leetcode.com/problems/subsets/discuss/329218/Python-DP
66

77
import os
8+
import collections
9+
10+
11+
def subset_dp(nums: list) -> list:
12+
dp = collections.defaultdict(list)
13+
dp[1] = [[i] for i in nums]
14+
15+
for x in range(2, len(nums)+1):
16+
for up in dp[x-1]:
17+
for i in range(nums.index(up[-1])+1, len(nums)):
18+
dp[x].append(up + [nums[i]])
19+
20+
ans = [[]]
21+
for val in dp.values():
22+
for v in val:
23+
ans.append(v)
24+
return ans
25+
26+
27+
class Solution:
28+
def subsets(self, nums: list) -> list:
29+
return subset_dp(nums)
830

931

1032
def main():
11-
pass
33+
nums = [1, 2, 3, 4]
34+
ans = subset_dp(nums)
35+
print(ans)
1236

1337

1438
if __name__ == "__main__":
15-
pass
39+
main()

source/leetcode/subset.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Subset 子集
2+
3+
### 动态规划法
4+
dp[i] means the subsets of length i.
5+
6+
For example,
7+
input: [1,2,3,4]
8+
dp[1] = [[1],[2],[3],[4]]
9+
10+
For dp[2], we can construct from [1]
11+
对于dp[2],用[1]组合起来
12+
[1][2],[3],[4]组合,[2][3],[4]组合, 即总是和后面的组合,这符合人手写组合的习惯
13+
对于dp[3],用dp[2]和dp[1]组合起来
14+
dp[2]这个时候就是dp[2] = [1,2], [1,3], [1,4], [2,3], [2,4], [3,4],那么
15+
[1,2] 组合 [3][4]
16+
17+
有个组合次序的问题,[1,2,3,4]的dp[3]里,[1,2,3]可以由[1,2][3]而成,也可以由[1,3][2]而成
18+
在上面的动态规划过程中,[1,2]组了[3][4][1,3]就只组[4],不组[3]
19+
也就是说,每次生成的上一级的子集都是有顺序的,对于同一个生成的集合,下一级的子集里,前面的子集优先组合,后面的子集则组剩下的
20+
dp[3] = [1,2,3],[1,2,4],[1,3,4]是怎么生成的呢,都去跟[1] [2] [3] [4]组合就行了,但是从[1,2]的位置看,只用遍历[3]开始的最小元素就行了
21+
[1,2,3],[1,2,4][1,2]组合[3][4]而成,而对于[1,3][1,2,3]已经被组合成了,只组剩下的[4],这样就避免了上一级子集重复生成的问题
22+
23+
### DFS法
24+
25+
26+
### 二进制法
27+
若 {\displaystyle S} S是有限集,有 {\displaystyle |S|=n} |S|=n个元素,那么 {\displaystyle S} S的幂集有 {\displaystyle |{\mathcal {P}}(S)|=2^{n}} |{\mathcal {P}}(S)|=2^{n}个元素。(其实可以——事实上电脑就是这样做的——将 {\displaystyle {\mathcal {P}}(S)} {\mathcal {P}}(S)的元素表示为n位二进制数;第n位表示包含或不含 {\displaystyle S} S的第n个元素。这样的数总共有 {\displaystyle 2^{n}} 2^{n}个。)——维基百科
28+
29+
30+
## 参考
31+
[https://leetcode.com/problems/subsets/discuss/329218/Python-DP](https://leetcode.com/problems/subsets/discuss/329218/Python-DP)
32+
[维基:幂集](https://zh.wikipedia.org/wiki/%E5%86%AA%E9%9B%86)

update.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22
git add -A
3-
git commit -am "update `date`"
3+
git commit -am "subset binary"
44
git push
55
if which gitbook > /dev/null; then
66
cd source

0 commit comments

Comments
 (0)