78. Subsets && 90. Subsets II
来源:互联网 发布:淘宝app 打包工具 编辑:程序博客网 时间:2024/05/21 11:33
Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> r = new ArrayList<List<Integer>>(); r.add(new ArrayList<Integer>()); if (nums == null || nums.length == 0) return r; Arrays.sort(nums); for (int i : nums) { List<List<Integer>> tmp = new ArrayList<List<Integer>>( ); for(List<Integer> t : r){ List<Integer> x = new ArrayList<Integer>(t); x.add(i); tmp.add(x); } r.addAll(tmp); } return r; }
Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
处理重复数字
public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> r = new ArrayList<List<Integer>>(); List<List<Integer>> change = new ArrayList<List<Integer>>(); //记录前一次操作新增的部分 if(nums == null || nums.length == 0) return r; Arrays.sort(nums); r.add(new ArrayList<Integer>()); for(int i = 0;i < nums.length;i++){ List<List<Integer>> tmp = new ArrayList<List<Integer>>(); //重复。只改变前一次操作中新增的部分;前一次操作已经存在的部分已经增加过重复的数字了 if(i > 0 && nums[i] == nums[i-1]){ for(List<Integer> t : change){ List<Integer> x = new ArrayList<Integer>(t); x.add(nums[i]); tmp.add(x); } } else{ for(List<Integer> t : r){ //没有重复 List<Integer> x = new ArrayList<Integer>(t); x.add(nums[i]); tmp.add(x); } } r.addAll(tmp); change = tmp; } return r; }
0 0
- 78. Subsets && 90. Subsets II
- 78. Subsets 90. Subsets II
- 78. Subsets &90. Subsets II
- 78. Subsets,90. Subsets II
- 78. Subsets && 90. Subsets II
- 78. Subsets 90. Subsets II
- 78. Subsets & 90. Subsets II
- Leetcode 78. Subsets & 90. Subsets II
- leetcode题解-78. Subsets && 90. Subsets II
- Leetcode 78. Subsets & 90. Subsets II
- [4]78. Subsets/90. Subsets II(Java)
- 78. Subsets I && 90. Subsets II
- 90. Subsets II
- [LeetCode]90.Subsets II
- [Leetcode] 90. Subsets II
- LeetCode --- 90. Subsets II
- 90. Subsets II
- [leetcode] 90.Subsets II
- mysql 各种级联查询后更新(update select)
- BroadReceiver之实现开机自动运行Service
- 在images.xcassets中存放图片的优势
- [c++]类实现年收入计算
- OpenStack设计与实现(一)虚拟化
- 78. Subsets && 90. Subsets II
- Halloween Costumes (LightOJ 1422)
- Android apk退出时与OMX Component之间的交互
- 冒泡排序java
- linux内核中的C语言常规算法(前提:你的编译器要支持typeof和type)
- 虚拟机中Hadoop分布式环境搭建
- Linux_定制自己的vim
- 利用waveIn完成简单录音(录成wav)
- hadoop - hadoop2.6 伪分布式 - 全局配置 和 启用 YARN 进行任务调度与资源管理