Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 如何找到给定数组的所有可能子集?
我必须找到给定数组的所有可能子集。你知道任何算法吗?
这是一个长镜头,但如果您需要一个函数来生成 {},{1},{2},{3},{1,2},{2,3},{1,3},{1, 2,3} 从 {1,2,3}。您可以生成从 0 到 2^count(array)-1 的二进制数字,并选择与生成数字中的二进制数字相对应的数组项。
000 -> {} 100 -> {1} 010 -> {2} 110 -> {1,2} 001 -> {3} 101 -> {1,3} 011 -> {2,3} 111 -> {1,2,3}
// 左侧二进制系统又名惰性二进制系统