lintcode 中等题:subSets 子集
题目
子集
给定一个含不同整数的集合,返回其所有的子集
如果 S = [1,2,3],有如下的解:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
子集中的元素排列必须是非降序的,解集必须不包含重复的子集
你可以同时用递归与非递归的方式解决么?
解题
根据上面求排列的思想很类似,还是深度优先遍历。由于输出每个子集需要升序,所以要先对数组进行排序。求出所以的子集,也就是求出所以的组合方式 + 空集
问题转化为求组合方式的问题
参考链接不仅要考虑起始位置,还需要考虑长度,这样才是组合 C(n,k),由于我只想到要考虑起始位置,而长度问题在程序中增加,一直没有解决问题
核心程序
public void helper(int[] nums,int start,int len,
ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){
if( list.size() == len){
res.add(new ArrayList<Integer>(list));
return;
}
for(int i=start;i< nums.length;i++){
if(list.contains(nums[i])){
continue;
}
list.add(nums[i]);
helper(nums,i+1,len,list,res);
list.remove(list.size()-1);
} }
class Solution {
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(nums == null || nums.length ==0)
return res;
Arrays.sort(nums);
res.add(list);
for(int len = 1;len<= nums.length;len++){
helper(nums,0,len,list,res);
}
return res;
}
public void helper(int[] nums,int start,int len,
ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){
if( list.size() == len){
res.add(new ArrayList<Integer>(list));
return;
}
for(int i=start;i< nums.length;i++){
if(list.contains(nums[i])){
continue;
}
list.add(nums[i]);
helper(nums,i+1,len,list,res);
list.remove(list.size()-1);
}
}
}
Java Code
九章中程序进行了优化,长度不考虑,递归一次list的值都是子集的一个元素
class Solution {
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(nums == null || nums.length ==0)
return res;
Arrays.sort(nums);
helper(nums,0,list,res);
return res;
}
public void helper(int[] nums,int start, ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){
res.add(new ArrayList<Integer>(list));
for(int i=start;i< nums.length;i++){
if(list.contains(nums[i])){
continue;
}
list.add(nums[i]);
helper(nums,i+1,list,res);
list.remove(list.size()-1);
}
}
}
Java Code
class Solution:
"""
@param S: The set of numbers.
@return: A list of lists. See example.
"""
def subsets(self, S):
def dfs(depth, start, valuelist):
res.append(valuelist)
if depth == len(S): return
for i in range(start, len(S)):
dfs(depth+1, i+1, valuelist+[S[i]])
S.sort()
res = []
dfs(0, 0, [])
return res
Python Code
根据位运算进行求解
// 1 << n is 2^n
// each subset equals to an binary integer between 0 .. 2^n - 1
// 0 -> 000 -> []
// 1 -> 001 -> [1]
// 2 -> 010 -> [2]
// ..
// 7 -> 111 -> [1,2,3]
下面是我自己实现
class Solution {
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
// write your code here
int len = nums.length;
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(nums == null || len==0)
return res;
Arrays.sort(nums);
// 1 << n is 2^n
// each subset equals to an binary integer between 0 .. 2^n - 1
// 0 -> 000 -> []
// 1 -> 001 -> [1]
// 2 -> 010 -> [2]
// ..
// 7 -> 111 -> [1,2,3]
for(int i=0;i< 1<<len ;i++){
ArrayList<Integer> list = new ArrayList<Integer>();
// 检测哪一位是 1
int n = i;
for(int j=0;j< len;j++){
if(n%2==1)
list.add(nums[j]);
n=n/2;
}
res.add(list);
}
return res;
}
}
九章中判断第几位是1的程序如下:
for (int i = 0; i < (1 << n); i++) {
ArrayList<Integer> subset = new ArrayList<Integer>();
for (int j = 0; j < n; j++) {
// check whether the jth digit in i's binary representation is 1
if ((i & (1 << j)) != 0) {
subset.add(nums[j]);
}
}
result.add(subset);
}
是懂非懂,好像这样也可以判断第几位是1
class Solution:
"""
@param S: The set of numbers.
@return: A list of lists. See example.
"""
def subsets(self, nums):
# write your code here
res = []
size = len(nums)
nums.sort()
if nums == None or size == 0:
return res
for i in range(1<<size):
lst=[]
n = i
for j in range(size):
if n%2==1:
lst.append(nums[j])
n/=2
res.append(lst)
return res
Python Code
lintcode 中等题:subSets 子集的更多相关文章
- lintcode 中等题:subsets II 带重复元素的子集
题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...
- lintcode 中等题:partition array 数组划分
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
- lintcode 中等题:permutations II 重复数据的全排列
题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- lintcode 中等题: Implement Trie
题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assu ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- lintcode 中等题:A + B Problem A + B 问题
题目: 中等 A + B 问题 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 如果 a=1 并且 b=2,返回3 注意 你不需要从输入流读入数据,只需要根据aplusb的两个参数 ...
- lintcode 中等题:搜索旋转排序数组II
题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...
随机推荐
- IE8浏览器跨域接口访问异常的解决办法
IE8版本以下浏览器绝对是一个神奇的存在,忙碌好久,万事具备,居然在ajax调用接口的时候直接爆炸 陈述一下问题 首先是有这样一个接口,请求类型POST,入参JSON,出参JSON,jQuery aj ...
- 【Sharing】开发与研发
[声明]此文为转载,只为收藏. 按:这几天我一直在写这篇东西,本来是胸有成竹,没想到后来越写越发现自己在这个题目下有太多话想说,而以我现在的能力又不能很好地概括总结,以至于越写越长,文章结构也变得混乱 ...
- delphi下,不错的多语言翻译组件
http://yktoo.com/en/software/dklangTraned http://sourceforge.net/projects/dklang/
- spring的配置
web.xml的配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...
- js关闭页面(兼容浏览器)
function closewindow() { window.opener = null; window.open("", "_self"); window. ...
- Python开发【第一篇】Python基础之自定义模块和内置模块
为什么要有模块,将代码归类.模块,用一砣代码实现了某个功能的代码集合. Python中叫模块,其他语言叫类库. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代 ...
- MySQL 5.7.11 重置root密码
.修改/etc/my.conf,添加参数skip-grant-tables .重启mysql service mysqld stop service mysqld start .用root 直接登录 ...
- 用户登录密码RSA加密后传输的实现,非明文密码传输
在用户登录页面,用户输入密码后,在传送到服务器端时,为防止在密码传送过程中,被如360这种东东给拦截到, 需要在传送前对密码进行加密,然后再传送! 利用RSA加密,在客户端使用公钥对密码进行加密,在服 ...
- Cisco IOS Basic CLI Configuration:Access Security 01
1. Telnet Switch Config: Switch>en Switch#conf t Enter configuration commands, one per line. En ...
- BI的核心价值[转]
BI的核心价值是辅助决策,从一个洁净的数据源中自动提取有价值的数据进行分析,从而成为重要商业决定的决策基础.但在国内,洁净的数据源不易得到,很多情况下都需要进行数据清洗,所以BI的应用受到很大程度的抑 ...