LeetCode46_Permutations
2017-09-25 17:57
330 查看
Permutations
问题描述
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] ]
简单介绍
这道题就是求排列组合问题中的排列问题,在前面LeetCode31 Next Permutation 这道题目中是求下一个排列数。可能会联想这两道题有什么关系,但是实际上解法没有关系。在这道题目中采用动态规划的做法就可以解决问题,简单来说把这些数字组成所有的排列。采用动态规划的话需要维护一个数组dp[i]代表的是数组中的第i个数字有没有被使用。
具体代码如下
代码
public void help(List<Integer> temp,int[] nums,boolean[] dp){ if(temp.size()==nums.length){ List<Integer> a = new ArrayList<>(temp); result.add(a); }else { for(int i =0;i<nums.length;i++){ if(!dp[i]){ temp.add(nums[i]); dp[i]=true; help(temp,nums,dp); temp.remove(temp.size()-1); dp[i]=false; } } } }
LeetCode学习笔记持续更新,GitHub地址
https://github.com/yanqinghe/leetcode
相关文章推荐
- LeetCode 46 Permutations (全排列)
- leetcode 46 Permutations
- [leetcode 46] Permutations------数组中元素的所有排列组合集合
- LeetCode-46 Permutations(全排列)
- leetcode 46: Permutations
- leetcode-46、47 Permutations/II 数字的排列组合
- leetcode[46]Permutations
- LeetCode力扣之46_Permutations
- Leetcode 46 Permutations
- 【Leetcode-Medium-46】Permutations
- 【LEETCODE】46-Permutations
- leetcode 46: permutations
- 【LeetCode】C# 46、Permutations
- LeetCode_46---Permutations
- LeetCode 46 Permutations + LeetCode 47 Permutations II
- LeetCode46,47 Permutations, Permutations II
- Leetcode 46 - Permutations
- [leetcode 46] Permutations
- leetcode || 46、Permutations
- leetcode46/47-Permutations I/II(全排列问题)