标签:des style blog http color io os ar java
题目:
Given a set of distinct integers, S, return all possible subsets.
Note:
For example,
If S = [1,2,3], a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
这里给出一个java代码:
import java.util.*;
public class Solution {
public List<List<Integer>> subsets(int[] S) {
int totalLength = 1<<S.length; //总共的子数组的个数
List<List<Integer>> l = new ArrayList<List<Integer>>();
Arrays.sort(S);//数组进行排序
for (int i=0;i<totalLength;i++){
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int j=0;j<S.length;j++){
if(((i >> j) & 1 )!= 0){ //将s[n]分别用二进制的n-1位代替
tmp.add(S[j]);
}
}
l.add(tmp);
}
return l;
}
//测试主函数
public static void main(String[] args) {
int[] array = {1,2,3};
Solution s = new Solution();
System.out.println(s.subsets(array));
}
}
if(((i >> j) & 1 )!= 0)
看一个图,如下
上图中以S=[1 2 3 4]为例子,可以看到一个规律,就是在前面所有的subset的最后再添加一个位数,就成了新生成的subset。
或者,我们简单的一理解为,s[0]代码二级制数字最后一位为1,s[1]代码二进制数字倒数第二位为1,如此下去。
如上图中,倒数第二个subset为[2 3 4],那么这代表着其用二级制表示为1110 = 14 这好是其外围数组的下标,这样不能理解上面列出的那个段代码了(或者如下)
if(((i >> j) & 1 )!= 0)
由上面图中的原理,这样就比较好的理解下面的python代码了:
def subset1(S):
A=[[]]
S.sort()
for n in S:
for i in range(len(A)):
ss = A[i][:]
ss.append(n)
A.append(ss)
return A下面的python在原理上再效率上会有所提高:
def subset2(S):
res = [[]]
S.sort()
for n in S:
res = res + [x + [n] for x in res]
return res标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/wolinxuebin/article/details/40433729