subsetII leetcode c++

I made two mistakes in this problem.

1.I forget to push_back the cur to answer when the cur.size() == num.

2.the change from this problem to subsets is just we need to write a judgement that in the same layer of the num, if S[i] == S[i-1] ,we just ignore the S[i].because this cause the duplicates. however,when I finished the judgement, I forget to write continue to make this judgement take effect.

class Solution {
public:
    vector<vector<int> > subsetsWithDup(vector<int> &S) {
    	vector<vector<int>> answer;
    	vector<int> cur;
    	answer.push_back(cur);
    	if(S.empty())
    		return answer;
    	sort(S.begin(),S.end());
    	for(int i = 1;i<=S.size();i++)
    		dfs(0,i,S,cur,answer);
    	return answer;  
    }
    void dfs(int start,int num,vector<int> &S,vector<int> &cur,vector<vector<int>> &answer)
    {
    	if(cur.size() == num)
    	{
    		answer.push_back(cur);
    		return;
    	}
    	for(int i = start;i<S.size();i++)
    	{
    		if(i!= start && S[i] == S[i-1])
    		continue;
    		cur.push_back(S[i]);
    		dfs(i+1,num,S,cur,answer);
    		cur.pop_back();
    	}
    }
}; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值