[Algorithm] 九章一:算法面试与代码风格
来源:互联网 发布:武藏s300点编程 编辑:程序博客网 时间:2024/06/07 01:41
Given a set of distinct integers, return all possible subsets.
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
Example
If S = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
Challenge :Can you do it in both recursively and iteratively?
思路:dfs
public class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> results=new ArrayList<>(); if(nums==null){ return results; } if(nums.length==0){ results.add(new ArrayList<Integer>()); return results; } Arrays.sort(nums); helper(new ArrayList<Integer>(),nums,0,results); return results; } private void helper(ArrayList<Integer> subset,int [] nums,int startIndex,List<List<Integer>> results){ results.add(new ArrayList<>(subset)); for(int i=startIndex;i<nums.length;i++){ subset.add(nums[i]); //例如[]->[1] helper(subset,nums,i+1,results); //寻找所有以1开头的子集,并加入到results里,再同样做子任务 subset.remove(subset.size()-1); } }}
0 0
- [Algorithm] 九章一:算法面试与代码风格
- 开发与面试涉及的基础数据结构和算法-Algorithm
- C++代码风格与规范
- C++代码风格与规范
- java代码规范与风格
- Algorithm(算法
- 算法(algorithm):
- Algorithm算法
- 模仿绘画风格的算法:A Neural Algorithm of Artistic Style
- 【转】模仿绘画风格的算法:A Neural Algorithm of Artistic Style
- 【面试】一些排序算法代码
- bandit算法(1)--epsilon-Greedy Algorithm(附代码)
- 代码风格(四)空格与空行
- 关于编程命名规则与代码风格
- 代码布局与风格(一)
- js代码风格与优美的特性
- 算法库algorithm-1-algorithm
- 代码风格
- 并发方法之 非阻塞同步算法与CAS(Compare and Swap)无锁算法
- 【数据结构与算法】小于等于k的最大连续子序列和
- LDA Gibbs Sampling公式推导
- 【Leetcode】363. Max Sum of Rectangle No Larger Than K
- 切片用例笔记
- [Algorithm] 九章一:算法面试与代码风格
- 高中数学:如何计算八个描边圆的位置
- 在没有好方法之前,坚持,成了暂时的别无选择。
- CSDN编辑器语法
- Python轻松入门-24 解包列表
- 后缀.do和.action
- Jenkins 学习
- HDU1715 大菲波数
- JVM学习之对象的创建