TreeviewCopyright © aleen42 all right reserved, powered by aleen42

46. 全排列

https://leetcode-cn.com/problems/permutations/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-10-27 15:42:40
 * @LastEditTime: 2020-10-27 15:43:10
 * @Description: https://leetcode-cn.com/problems/permutations/
 * @FilePath: \leetcode-googtech\#46. Permutations\Solution.java
 * @WebSite: https://algorithm.show/
 */
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;
    }

    // BackTrack : 回溯算法
    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))
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""