subsets/subsets II/Partition Equal Subset Sum/Partition to K Equal Sum Subsets

来源:互联网 发布:php 广告发布系统源码 编辑:程序博客网 时间:2024/06/07 06:02

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

代码如下:改代码在leetcode可以ac,在牛客网不能,因为res的顺序不一样

vector<vector<int>> subsets(vector<int>& nums) {        sort(nums.begin(),nums.end());        vector<vector<int>> res;        vector<int> tmp;        dfs(nums,res,tmp,0);        return res;    }    void dfs(vector<int> &s,vector<vector<int>> &res,vector<int> &tmp,int begin)    {        res.push_back(tmp);        for(int i=begin;i<s.size();i++)        {            tmp.push_back(s[i]);            dfs(s,res,tmp,i+1);            tmp.pop_back();        }    }

第二题:
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
代码如下:

class Solution {public:    vector<vector<int>> subsetsWithDup(vector<int>& nums) {        vector<vector<int>> res;        vector<int> tmp;        sort(nums.begin(),nums.end());        dfs(nums,res,tmp,0);        return res;    }    void dfs(vector<int> &s,vector<vector<int>> &res,vector<int> &tmp,int begin)    {        res.push_back(tmp);        for(int i=begin;i<s.size();i++)        {            if(i>begin && s[i]==s[i-1]) continue;            tmp.push_back(s[i]);            dfs(s,res,tmp,i+1);            tmp.pop_back();        }    }};

第三题:
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Note:
Each of the array element will not exceed 100.
The array size will not exceed 200.
Example 1:

Input: [1, 5, 11, 5]

Output: true

Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:

Input: [1, 2, 3, 5]

Output: false

Explanation: The array cannot be partitioned into equal sum subsets.

代码1:这样做时间超时,但思想本身没有问题

class Solution {public:    bool canPartition(vector<int>& nums) {        int sum=0;        for(int i=0;i<nums.size();i++)            sum+=nums[i];        if(sum%2==1)            return false;       sort(nums.begin(),nums.end());        bool flag=0;        int partsum=0;        dfs(nums,partsum,0,flag,sum/2);        return flag;    }    void dfs(vector<int> &s,int &partsum,int begin,bool &flag,int target)    {        if(partsum==target)        {            flag=1;            return;        }        for(int i=begin;i<s.size();i++)        {            if(i>begin && s[i]==s[i-1])                continue;            partsum+=s[i];            dfs(s,partsum,i+1,flag,target);            partsum-=s[i];        }    }};

代码2:可以ac,动态规划(抄的别人的)
思路:这道题给了我们一个数组,问我们这个数组能不能分成两个非空子集合,使得两个子集合的元素之和相同。那么我们想,原数组所有数字和一定是偶数,不然根本无法拆成两个和相同的子集合,那么我们只需要算出原数组的数字之和,然后除以2,就是我们的target,那么问题就转换为能不能找到一个非空子集合,使得其数字之和为target。开始我想的是遍历所有子集合,算和,但是这种方法无法通过OJ的大数据集合。于是乎,动态规划DP就是我们的不二之选。我们定义一个一维的dp数组,其中dp[i]表示数字i是否是原数组的任意个子集合之和,那么我们最后只需要返回dp[target]就行了。我们初始化dp[0]为true,由于题目中限制了所有数字为正数,那么我们就不用担心会出现和为0或者负数的情况。那么关键问题就是要找出递归公式了,我们需要遍历原数组中的数字,对于遍历到的每个数字nums[i],我们需要更新我们的dp数组,要更新[nums[i], target]之间的值,那么对于这个区间中的任意一个数字j,如果dp[j - nums[i]]为true的话,那么dp[j]就一定为true,于是地推公式如下:

dp[j] = dp[j] || dp[j - nums[i]] (nums[i] <= j <= target)

有了递推公式,那么我们就可以写出代码如下:

bool canPartition(vector<int>& nums) {    int sum = accumulate(nums.begin(), nums.end(), 0);     if (sum & 1) return false;    int target = sum >> 1;    vector<int> dp(target + 1, 0);    dp[0] = 1;    for(auto num : nums)         for(int i = target; i >= num; i--)            dp[i] = dp[i] || dp[i - num];    return dp[target];}

代码3:可以ac,位运算

bool canPartition(vector<int>& nums) {    bitset<5001> bits(1);    int sum = accumulate(nums.begin(), nums.end(), 0);    for (auto n : nums) bits |= bits << n;    return !(sum & 1) && bits[sum >> 1];}

第四题:Partition to K Equal Sum Subsets
Given an array of integers nums and a positive integer k, find whether it’s possible to divide this array into k non-empty subsets whose sums are all equal.

Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It’s possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:

1 <= k <= len(nums) <= 16.
0 < nums[i] < 10000.

代码如下:

原创粉丝点击