美文网首页
46. 全排列

46. 全排列

作者: 名字是乱打的 | 来源:发表于2021-07-19 22:36 被阅读0次

题目:

思路 回溯

代码:

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        LinkedList<List<Integer>> res = new LinkedList<>();
        getRes(nums,res, new LinkedList<Integer>(),nums.length);
        return res;
    }

    private void getRes(int[] nums,LinkedList<List<Integer>> lists, LinkedList<Integer> path,int len) {
        if (path.size()==len){
            lists.add(new LinkedList<>(path));
            return;
        }

        for (int i = 0; i < len; i++) {
            if (path.contains(nums[i])){
                continue;
            }else {
                path.add(nums[i]);
                getRes(nums,lists,path,len);
                path.removeLast();
            }
        }
    }
}

相关文章

网友评论

      本文标题:46. 全排列

      本文链接:https://www.haomeiwen.com/subject/cqdkmltx.html