子集

78. 子集

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: nums = [1,2,3]
输出:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
package gongel_first;

import java.util.ArrayList;
import java.util.List;

public class Seventy_eight {
    /**
     * 法一:递归+回溯法
     */
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<Integer>();
        subsets(res, list, nums, 0);
        return res;
    }

    public void subsets(List<List<Integer>> res, List<Integer> temp, int[] nums, int start) {
        res.add(new ArrayList<>(temp));
        if (start == nums.length)
            return;
        for (int i = start; i < nums.length; i++) {
            temp.add(nums[i]);
            subsets(res, temp, nums, i + 1);
            temp.remove(temp.size() - 1);
        }
    }


    /**
     * 法二:位运算
     */
    public List<List<Integer>> subsets2(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        int end = (int) Math.pow(2, nums.length);
        for (int i = 0; i < end; i++)
            ans.add(helper(nums, i));
        return ans;
    }

    //把n化为二进制,0表示不取,1表示取
    public List<Integer> helper(int[] nums, int n) {
        List<Integer> ans = new ArrayList<>();
        int i = 0;
        while (n != 0) {
            if ((n & 1) == 1)//判断二进制最后一位是否为1
                ans.add(nums[i]);
            i++;
            n >>= 1;//右移1位去除最后1位二进制
        }
        return ans;
    }
}
class Solution {
public:
    vector<vector<int>> subsets(vector<int> &nums) {
        if (nums.empty())
            return vector<vector<int>>();
        vector<int> temp;
        vector<vector<int>> ans;
        helper(nums, ans, temp, 0);
        return ans;
    }

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

90. 子集 II

给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

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

package gongel_first;

import java.util.*;

public class Ninety {
    /**
     * 递归+回溯+去重
     */
    public void dfs(int[] nums, int start, List<Integer> temp, List<List<Integer>> ans) {
        if (!ans.contains(temp))
            ans.add(new ArrayList<>(temp));
        for (int i = start; i < nums.length; i++) {
            temp.add(nums[i]);
            dfs(nums, i + 1, temp, ans);
            temp.remove(temp.size() - 1);
        }
    }

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if (nums == null || nums.length == 0)
            return new ArrayList<>();
        Arrays.sort(nums);
        List<Integer> temp = new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        dfs(nums, 0, temp, ans);
        return ans;
    }
}
class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int> &nums) {
        if (nums.empty())
            return vector<vector<int>>();
        vector<int> temp;
        set<vector<int>> ans;
        sort(nums.begin(), nums.end());
        helper(nums, ans, temp, 0);
        return vector<vector<int>>(ans.begin(), ans.end());
    }

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

 

0

全排列

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