SubSets,SubSets2, 求数组所有子集
问题描述:
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
算法分析:第一种方法循环遍历方法,每次在原有集合基础上添加新元素。第二种算法:该类问题属于Backtraking回溯算法。
public class SubSets
{
//循环遍历法
public ArrayList<ArrayList<Integer>> subsets(int[] s)
{
if(s == null)
{
return null;
} Arrays.sort(s); ArrayList<ArrayList<Integer>> res = new ArrayList<>(); for(int i = 0; i < s.length; i ++)
{
ArrayList<ArrayList<Integer>> temp = new ArrayList<>();
//get sets that are already in result
for (ArrayList<Integer> a : res)
{
temp.add(new ArrayList<Integer>(a));
}
//add S[i] to existing sets
for (ArrayList<Integer> a : temp)
{
a.add(s[i]);
}
//add S[i] only as a set
ArrayList<Integer> single = new ArrayList<>();
single.add(s[i]);
temp.add(single); res.addAll(temp);
}
//add empty set
res.add(new ArrayList<Integer>()); return res;
} //回溯法
public List<List<Integer>> subsets_backtaking(int[] nums)
{
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
dfs(res, new ArrayList<>(), nums, 0);
return res;
}
public void dfs(List<List<Integer>> res, List<Integer> temp, int[] nums, int start)
{
res.add(new ArrayList<>(temp));
for(int i = start; i < nums.length; i ++)
{
temp.add(nums[i]);
dfs(res, temp, nums, i + 1);
temp.remove(temp.size() - 1);
}
}
}
SubSets2:数组里有重复元素
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
public class SubSets2
{
//回溯法
public List<List<Integer>> subsets(int[] nums)
{
if(nums == null)
{
return null;
}
Arrays.sort(nums);//千万别忘了排序,否则就出错了,因为这个数组里面有重复元素
List<List<Integer>> res = new ArrayList<>();
dfs(res, new ArrayList<>(), nums, 0);
return res;
}
public void dfs(List<List<Integer>> res, List<Integer> temp, int[] nums, int start)
{
res.add(new ArrayList<>(temp));
for(int i = start; i < nums.length; i ++)
{
//skip duplicates
if(i > start && nums[i] == nums[i-1])
{
continue;
}
temp.add(nums[i]);
dfs(res, temp, nums, i + 1);
temp.remove(temp.size() - 1);
}
}
}
SubSets,SubSets2, 求数组所有子集的更多相关文章
- [Leetcode] subsets ii 求数组所有的子集
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [Leetcode] subsets 求数组所有的子集
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- C++ 数组长度 以及 数组名作为参数传递给函数 以及 为什么不在子函数中求数组长度
在看排序,首先是插入排序,思路理清后想用代码实现,然后问题来了: 如何求数组长度? 如果没记错,在Java中应该是有直接可用的方法的, Python中(序列)也有.len,在C/C++中,字符串倒是有 ...
- C#中求数组的子数组之和的最大值
<编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...
- 《github一天一道算法题》:分治法求数组最大连续子序列和
看书.思考.写代码. /*************************************** * copyright@hustyangju * blog: http://blog.csdn. ...
- PHP使用array_intersect()函数求数组交集
在PHP中求数组的交集,我们可以与PHP给我们提供的现成函数:array_intersect(),其用法格式为: array array_intersect(array array1,array ar ...
- 求数组的最小数、最大值,求一组数的平均数,sort函数详解,类数组转数组
求数组的最小值和最大值 //求数组当中最大值和最小值 var arr=[3,2,6,1,45,23,456,23,2,6,3,45,37,89,30]; //第一种方法 根据排序方法来求最大值和最小值 ...
- C陷阱:求数组长度
// 这是一篇导入进来的旧博客,可能有时效性问题. 程序中,当我们建立了一个int型数组:int a[]={1,2,3,4,5,6};随后我们可能需要知道它的长度,此时可以用这种方法:length = ...
随机推荐
- .NET面试
作者:最佳菜鸟链接:https://zhuanlan.zhihu.com/p/22224795来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 1 .术语 面试出现频率: ...
- 巨蟒python全栈开发flask3
首先,我们新建一个项目: 这个时候,我们调用ab函数,可以在所有的模板中使用. 上边是一个特殊装饰器, 1.flask特殊装饰器 下面说几个特殊的装饰器 再请求之前的装饰器 运行: 这个时候,服务端打 ...
- C# WinForm 中进行UrlEncode
public static string ToUrlEncode(string strCode) { StringBuilder sb = new StringBuilder(); byte[] by ...
- php strtok()函数用法,及使用时遇到的问题
strtok()函数:用来将一段字符串分割为子字符串 strtok(string $str, string $token) strtok( string $token) //仅第一次调用$str,以后 ...
- macro-name replacement-text 宏 调试开关可以使用一个宏来实现
C++ 预处理器_w3cschool https://www.w3cschool.cn/cpp/cpp-preprocessor.html C++ 预处理器 预处理器是一些指令,指示编译器在实际编译之 ...
- Checksum 磁盘扇区故障检测
w https://en.wikipedia.org/wiki/Checksum https://zh.wikipedia.org/wiki/校验和 A checksum is a small-siz ...
- GetWindowThreadProcessId
函数功能:该函数返回创建指定窗口线程的标识和创建窗口的进程的标识符,后一项是可选的. 函数原型:DWORD GetWindowThreadProcessld(HWND hwnd,LPDWORD lpd ...
- appium入门基础知识
1.概念区分: 1)IOS-UIAutomation:随着iOS4.0的发布,苹果公司同时发布了一个名为UIAutomation的测试框架,它可以用来在真实设备和iPhone模拟器上执行自动化测试 学 ...
- 正则表达式 \b
引用网上一段话: \b 是正则表达式规定的一个特殊代码(好吧,某些人叫它元字符,metacharacter),代表着单词的开头或结尾,也就是单词的分界处.虽然通常英文的单词是由空格,标点符号或者换行来 ...
- node.js---sails项目开发(4)---配置MongoDB数据库连接
1.安装sails对mongo的依赖 npm install sails-mongo --save 2. 配置mongo连接 修改config/connections.js: module.expor ...