LeetCode 46:Permutations

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

这题不知道出了什么意外,输入[0,1]在Eclipse上运行结果:[[0, 1], [1, 0]]

LeetCode上运行结果:[[1],[0,1],[1,0]]

简直了.....晕。是什么原因还有待解决。

public static List<List<Integer>> ans=new ArrayList<List<Integer>>();
    public static List<List<Integer>> permute(int[] nums) {
        int l=nums.length;
        List<Integer> temp=new ArrayList<Integer>();
        if(l==0)
        return ans;
        if(l==1){
            temp.add(nums[0]);
            ans.add(temp);
            return ans;
        }

        Arrays.sort(nums);
        dfs(temp, nums, new boolean[nums.length]);
        return ans;

    }

    public static void dfs(List<Integer> temp,int[] num,boolean [] used){
        if(temp.size() == num.length){
            ans.add(new ArrayList(temp));//add a copy
            return;
        }
        for(int i = 0; i < num.length; i++){
            //if(i > 0 && !used[i-1] && num[i] == num[i-1]) continue;// judge duplicate number
            if(!used[i]){
                temp.add(num[i]);
                used[i] = true;
                dfs(temp, num, used);
                temp.remove(temp.size() - 1);
                used[i] = false;
            }
    }
    }

时间: 2024-08-27 20:54:53

LeetCode 46:Permutations的相关文章

[LeetCode 46 &amp; 47] Permutations I &amp; II

题目链接:permutations 相似题型: 1. [LeetCode 39&40] Combination Sum I & II 2. [LeetCode 78] Subsets 3. [LeetCode 90] Subsets II 4. [LeetCode 22] Generate Parentheses 5. [LeetCode 77] Combinations import java.util.ArrayList; import java.util.List; /** * Gi

LeetCode OJ:Permutations(排列)

Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 排列组合的问题,不用考虑是否发生重复的情况,代码如下: 1 class Solution { 2 public: 3 vector<vector<i

leetcode || 46、Permutations

problem: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. Hide Tags Backtracking 题意:给定一个序列,输出其所有的排列组合 thinking: (1)之前写过求一个

[Leetcode 46]全排列 Permutations 递归

[题目] Given a collection of distinct integers, return all possible permutations. 数组的组合情况. Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] [思路] 求子集,排列组合的数组题有固定模板. [代码] class Solution { public List<List<Integer>> p

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

DFS解法的两道题 Leetcode 46 Permutations &amp; Leetcode 78 Subset

Leetcode 78 Subset Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1

LeetCode 46 Permutations(全排列问题)

题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组能够产生的所有全排列 采用递归算法,传入参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列 nums为最初的

LeetCode 46 Permutations (全排列)

Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 题目链接:https://leetcode.com/problems/permutations/ 题目大意:求全排列 题目分析:求

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&