leetCode-Subsets
来源:互联网 发布:沈阳市2016年经济数据 编辑:程序博客网 时间:2024/06/03 15:18
Description:
Given a set of distinct integers, nums, return all possible subsets (the power set).
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], []]
Solution:
public class Solution { public List<List<Integer>> subsets(int[] S) { List<List<Integer>> res = new ArrayList<>(); res.add(new ArrayList<Integer>()); Arrays.sort(S); for(int i : S) { List<List<Integer>> tmp = new ArrayList<>(); for(List<Integer> sub : res) { List<Integer> a = new ArrayList<>(sub); a.add(i); tmp.add(a); } res.addAll(tmp); } return res; }}
Better Solution:
public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if (nums == null || nums.length == 0) return res; res.add(new ArrayList()); int len = nums.length; for (int i = 0; i < len; i++){ int size = res.size(); for (int j = 0; j < size; j++){ ArrayList<Integer> temp = new ArrayList<Integer>(res.get(j)); temp.add(nums[i]); res.add(temp); } } return res;}}
阅读全文
0 0
- 【LeetCode】 Subsets Subsets II
- 【LeetCode】Subsets & Subsets II
- LeetCode Subsets && Subsets II
- LeetCode Subsets && Subsets II
- LeetCode-Subsets & Subsets II
- LeetCode: Subsets
- LeetCode Subsets
- [Leetcode] Subsets
- LeetCode : Subsets
- [LeetCode] Subsets
- Leetcode: Subsets
- Leetcode: Subsets ||
- [leetcode] subsets
- Leetcode - Subsets
- [LeetCode]Subsets
- LeetCode-Subsets
- LeetCode - Subsets
- LeetCode | Subsets
- Web集群与负载均衡服务实现
- 算法训练 9-7链表数据求和操作
- itop4412 uboot 学习详细记录(一丶Exynos4412 的启动)
- WEB页面_批量操作提交数据(批量删除)
- C++类库开发
- leetCode-Subsets
- 初学者的烦恼&&字符串右移
- MySQL多表与JDBC优化
- 数组右循环(指针)
- 将购买的域名跳转到博客首页,记录下方法
- Ubuntu 下生成git公钥
- 信息安全概论总结-4
- JQuery笔记
- 这16个数据可视化案例,惊艳了全球数据行业