Permutations && Permutations ii

本文详细解析了Permutations算法(不含重复元素排列)与PermutationsII算法(含重复元素排列)的实现原理及Java代码实现。Permutations算法通过深度优先搜索实现全排列,而PermutationsII算法在此基础上增加了跳过重复元素的功能,确保结果中没有重复的排列。

Permutations 中不带有重复元素

Permutations II 中带有重复元素


Permutations

java

public class Solution {
    /*
     * @param nums: A list of integers.
     * @return: A list of permutations.
     */
    public List<List<Integer>> permute(int[] nums) {
        // write your code here
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            result.add(new ArrayList<Integer>());
            return result;
        }
        List<Integer> path = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        dfs(nums, path, result, set);
        return result;
    }
    private void dfs(int[] nums, 
                     List<Integer> path,
                     List<List<Integer>> result,
                     Set<Integer> set) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<Integer>(path));
            return;
        }          
        for (int i = 0; i < nums.length; i++) {
            if (!set.contains(nums[i])) {
                set.add(nums[i]);
                path.add(nums[i]);
                dfs(nums, path, result, set);
                set.remove(nums[i]);
                path.remove(path.size() - 1);
            }
        }
    }
}


Permutations II

java

public class Solution {
    /*
     * @param :  A list of integers
     * @return: A list of unique permutations
     */
    public List<List<Integer>> permuteUnique(int[] nums) {
        // write your code here
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            result.add(new ArrayList<Integer>());
            return result;
        } 
        List<Integer> path = new ArrayList<>();
        boolean[] visit = new boolean[nums.length];
        Arrays.sort(nums);
        dfs(nums, path, result, visit);
        return result;
    }
    private void dfs(int[] nums, 
                     List<Integer> path, 
                     List<List<Integer>> result,
                     boolean[] visit) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<Integer>(path));
            return;
        }          
        for (int i = 0; i < nums.length; i++) {
            if ((visit[i] == true) || (i != 0 && nums[i] == nums[i - 1] && visit[i - 1] == false)) {
                continue;
            } else {
                path.add(nums[i]);
                visit[i] = true;
                dfs(nums, path, result, visit);
                visit[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值