• 47. 全排列 II


    给定一个可包含重复数字的序列,返回所有不重复的全排列。

    示例:

    输入: [1,1,2]
    输出:
    [
    [1,1,2],
    [1,2,1],
    [2,1,1]
    ]

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/permutations-ii
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    class Solution:
        def permuteUnique(self, nums: List[int]) -> List[List[int]]:
            return list(set(itertools.permutations(nums)))
    class Solution {
        public List<List<Integer>> permuteUnique(int[] nums) {
            List<List<Integer>> list = new ArrayList<>();
            Arrays.sort(nums);
            backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]);
            return list;
        }
    
        private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, boolean [] used){
            if(tempList.size() == nums.length){
                list.add(new ArrayList<>(tempList));
            } else{
                for(int i = 0; i < nums.length; i++){
                    if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue;
                    used[i] = true; 
                    tempList.add(nums[i]);
                    backtrack(list, tempList, nums, used);
                    used[i] = false; 
                    tempList.remove(tempList.size() - 1);
                }
            }
        }
    }
  • 相关阅读:
    P2403 [SDOI2010]所驼门王的宝藏
    差分约束系统
    题解报告——运输计划
    差分与树上差分
    题解报告——天使玩偶
    题解报告——Mokia
    CDQ分治&整体二分(未完待续)
    点分治
    AC自动机
    树链剖分
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13843207.html
Copyright © 2020-2023  润新知