import java.util.ArrayList;
import java.util.List;
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
for (int num : nums) {
List<List<Integer>> tmp = new ArrayList<>();
for (List<Integer> res : result) {
List<Integer> list = new ArrayList<>(res);
list.add(num);
tmp.add(list);
}
result.addAll(tmp);
}
return result;
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> result = new Solution().subsets(nums);
System.out.println(result);
}
}
[参考]https://discuss.leetcode.com/topic/9031/simple-java-solution-with-for-each-loops