全排列

46. 全排列

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
public class Forty_six {
    /**
     * 回溯
     */
    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0)
            return new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        dfs(ans, temp, nums);
        return ans;
    }

    public void dfs(List<List<Integer>> ans, List<Integer> temp, int[] nums) {
        if (temp.size() == nums.length) {
            ans.add(new ArrayList<>(temp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!temp.contains(nums[i])) {//temp暂时没有访问过的
                temp.add(nums[i]);
                dfs(ans, temp, nums);
                temp.remove(temp.size() - 1);
            }
        }
    }
}
class Solution {
public:
    vector<vector<int>> permute(vector<int> &nums) {
        vector<int> temp;
        vector<vector<int>> ans;
        helper(nums, temp, ans);
        return ans;
    }

    void helper(vector<int> &nums, vector<int> &temp, vector<vector<int>> &ans) {
        if (temp.size() == nums.size()) {
            ans.push_back(temp);
            return;
        }
        for (int i = 0; i < nums.size(); i++) {
            if (find(temp.begin(), temp.end(), nums[i]) == temp.end()) {
                temp.push_back(nums[i]);
                helper(nums, temp, ans);
                temp.pop_back();
            }
        }
    }
};

47. 全排列 II

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

示例:

输入: [1,1,2]
输出:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]
public class Forty_seven {
    /**
     * 回溯
     */
    public List<List<Integer>> permuteUnique(int[] nums) {
        if (nums == null || nums.length == 0)
            return new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        List<Integer> visited = new ArrayList<>();//在46题中,temp包含了此功能,但是本题有重复的值
        dfs(ans, temp, nums, visited);
        return ans;
    }

    public void dfs(List<List<Integer>> ans, List<Integer> temp, int[] nums, List<Integer> visited) {
        if (temp.size() == nums.length) {
            List<Integer> t = new ArrayList<>(temp);
            if (!ans.contains(t))//防止重复
                ans.add(t);
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!visited.contains(i)) {//visited暂时没有访问过的,不能用temp,因为有重复的值
                temp.add(nums[i]);
                visited.add(i);
                dfs(ans, temp, nums, visited);
                temp.remove(temp.size() - 1);
                visited.remove(visited.size() - 1);
            }
        }
    }
}
class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int> &nums) {
        if (nums.empty())
            return vector<vector<int>>();
        vector<int> temp;
        vector<int> visited;
        set<vector<int>> ans;
        helper(nums, temp, visited, ans);
        return vector<vector<int>>(ans.begin(), ans.end());
    }

    void helper(vector<int> &nums, vector<int> &temp, vector<int> &visited, set<vector<int>> &ans) {
        if (temp.size() == nums.size()) {
            ans.insert(temp);
            return;
        }
        for (int i = 0; i < nums.size(); i++) {
            if (find(visited.begin(), visited.end(), i) == visited.end()) {
                visited.push_back(i);
                temp.push_back(nums[i]);
                helper(nums, temp, visited, ans);
                temp.pop_back();
                visited.pop_back();
            }
        }
    }
};
0