TreeviewCopyright © aleen42 all right reserved, powered by aleen42
46. 全排列
https://leetcode-cn.com/problems/permutations/
Java
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> list = new ArrayList<>();
backtrack(result, list, nums);
return result;
}
public void backtrack(List<List<Integer>> result, List<Integer> list, int[] nums) {
if(list.size() == nums.length) {
result.add(new ArrayList<Integer>(list));
return;
}
for(int num : nums) {
if(!list.contains(num)) {
list.add(num);
backtrack(result, list, nums);
list.remove(list.size() - 1);
}
}
}
}
Python
'''
Author: Goog Tech
Date: 2020-10-27 15:42:46
LastEditTime: 2020-10-27 15:43:29
Description: https://leetcode-cn.com/problems/permutations/
FilePath: \leetcode-googtech\#46. Permutations\Solution.py
WebSite: https://algorithm.show/
'''
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
return list(itertools.permutations(nums))