【LeetCode】 046. Permutations
2017-01-12 23:16
405 查看
Given a collection of distinct numbers, return all possible permutations.
For example,
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
backTrack(res, new ArrayList<Integer>(), nums);
return res;
}
private void backTrack(List<List<Integer>> res, List<Integer> subList, int[] nums) {
if (subList.size() == nums.length) {
res.add(new ArrayList<Integer>(subList));
}
for (int i = 0; i < nums.length; i++) {
if (subList.contains(nums[i])) {
continue;
}
subList.add(nums[i]);
backTrack(res, subList, nums);
subList.remove(subList.size() - 1);
}
}
}
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] ]
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
backTrack(res, new ArrayList<Integer>(), nums);
return res;
}
private void backTrack(List<List<Integer>> res, List<Integer> subList, int[] nums) {
if (subList.size() == nums.length) {
res.add(new ArrayList<Integer>(subList));
}
for (int i = 0; i < nums.length; i++) {
if (subList.contains(nums[i])) {
continue;
}
subList.add(nums[i]);
backTrack(res, subList, nums);
subList.remove(subList.size() - 1);
}
}
}
相关文章推荐
- LeetCode 046 Permutations
- leetcode解题方案--046--Permutations
- LeetCode 046 Permutations
- [LeetCode]题解(python):046-Permutations
- 【LeetCode-面试算法经典-Java实现】【046-Permutations(求排列)】
- [LeetCode]046-Permutations
- leetcode 046 Permutations
- leetcode 046 —— Permutations
- Java for LeetCode 046 Permutations
- LeetCode 046 Permutations
- leetcode: Permutations I II
- Leetcode46 Permutations
- [LeetCode][JavaScript]Permutations
- [LeetCode] Permutations 解题报告
- [LeetCode69]Permutations
- LeetCode OJ Permutations
- 【leetcode】第46题 Permutations(递归法)题目+解析+代码
- [LeetCode 46 & 47] Permutations I & II
- Leetcode---Permutations
- 【leetcode刷提笔记】Permutations