46. Permutations【LeetCode算法之旅之DFS】
来源:互联网 发布:js embed 属性 编辑:程序博客网 时间:2024/05/01 14:25
Given a collection of distinct numbers, return all possible permutations.
For example,[1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]题意:求一个序列的各种组合。
思路:使用DFS策略。代码如下:
package leetcode;import java.util.ArrayList;import java.util.List;public class Solution { private List<List<Integer>> list = new ArrayList<List<Integer>>(); private void dfs(int[] nums, List<Integer> subList){ if(subList.size() == nums.length){ list.add(subList); return; } for (int i = 0; i < nums.length; i++){ if(!subList.contains(nums[i])){ List<Integer> subList2 = new ArrayList<Integer>(); subList2.addAll(subList);//subList2是对subList的复制 subList2.add(nums[i]); dfs(nums, subList2); } } } public List<List<Integer>> permute(int[] nums) { List<Integer> subList = new ArrayList<Integer>(); dfs(nums, subList); return list; }}
1 0
- 46. Permutations【LeetCode算法之旅之DFS】
- LeetCode算法题之Permutations
- LeetCode 46. Permutations DFS
- 【Leetcode】46. Permutations【DFS】
- [LeetCode]46. Permutations &&深搜DFS
- leetcode之Permutations
- leetcode之Permutations II
- 【Leetcode】之 Permutations
- 【Leetcode】之Permutations II
- LeetCode 之 Permutations
- 【LeetCode】马三来刷题之Permutations
- leetcode之Permutations问题
- 【LeetCode】 Permutations 排列生成算法之字典序法
- 每日算法之三十六:Permutations && Permutations II
- leetcode 之 Permutations 解题思路
- leetcode之路046 Permutations
- leetcode---permutations---dfs
- leetcode---permutations-ii---dfs
- L2-005. 集合相似度
- Code for K-Means
- php项目-美团网开发
- 深入分析Java的序列化与反序列化
- 2014燕十八PHP高性能架构班
- 46. Permutations【LeetCode算法之旅之DFS】
- Keil警告和错误语句与消除方法笔记
- ERROR 1067 (42000): Invalid default value for '***'
- 【C#/WPF】TextBlock/TextBox/Label编辑文字的问题
- (39)Air Band OpenCV_找到图像中的轮廓
- MySQL外键约束详解
- 七层登录
- JSP-SERVLET_知识点汇总
- HTML5+PhoneGap开发