Subsets II
来源:互联网 发布:windows文件恢复大师 编辑:程序博客网 时间:2024/06/06 03:04
题目
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S =[1,2,2]
, a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]
方法
利用DFS,一层相同的元素不要放入即可。public void getSets(int[] S, int start, int end, List<List<Integer>> list, List<Integer> subList) { list.add(subList); int pre = Integer.MIN_VALUE; for (int i = start; i < end; i++) { List<Integer> newSubList = new ArrayList<Integer>(subList); int cur = S[i]; if (pre != cur) { newSubList.add(S[i]); getSets(S, i + 1, end, list, newSubList); pre = cur; } } } public List<List<Integer>> subsetsWithDup(int[] num) { Arrays.sort(num); List<List<Integer>> list = new ArrayList<List<Integer>>(); List<Integer> subList = new ArrayList<Integer>(); getSets(num, 0, num.length, list, subList); return list; }
0 0
- 【LeetCode】 Subsets Subsets II
- 【LeetCode】Subsets & Subsets II
- LeetCode Subsets && Subsets II
- Subsets and Subsets II
- LeetCode Subsets && Subsets II
- LeetCode-Subsets & Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- Subsets II
- 用idea编写appium用例
- 浙江大华浙大2010年校园招聘笔试试题
- 链表类型list和foreard_list的特定容器算法
- 使用python编写appium用例
- ExtJs中Store简介(秘籍)
- Subsets II
- 利用cmd打开文件
- 《Javascript高级程序设计》第十三章——事件学习笔记(待续)
- ext中ArrayStore,JsonStore,XmlStore的用
- Compiling HTK 3.4 on Mac OS 10.5
- winform网络编程之TcpClient类,TcpListener类和UdpClient类
- 个人写的一个函数,高手勿喷
- ubuntu使用apt-get install 安装程序时候报同样Depends错误
- 阿伦:NBA应重评50大巨星 美记:"热刺"必数人入围