Subsets - LeetCode


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],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

我的CODE没有AC,因为OJ上的输出和我自己的测试输出不一样,在OJ上多次遇到这个问题,不知道是什么BUG。

对于{1,2}这个输入,我的Eclipse的输出是这样的:

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

但这是OJ反应的错误:

Input:[1,2]
Output:[[0],[],[1],[1,2],[2],[]]
Expected:[[],[1],[2],[1,2]]

比较值得注意的是,如果简单的用DFS进行操作,就会输出Permutation的结果,那么这里我们就要做一个小的变通,只要DFS找到一个合适的结果,我们就输出并Return,这样就没问题了。

还有插入的操作需要注意一下,不要让指针错误的改变了正确的结果。

ArrayList<Integer> rr = new ArrayList<Integer>(r);
	        result.add(rr);


以下是我的代码:

public class Solution {
	public static ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
	public static ArrayList<Integer> r = new ArrayList<Integer>();
	public static int count = 0; 
	public static void combinations(int k, int[]set, int used[]){
	    if(r.size() == k){
	        ArrayList<Integer> rr = new ArrayList<Integer>(r);
	        result.add(rr);
	        return;
	    }	
	    
	    for(int i = 0; i < set.length; i++)
	    	
	    	{
	        if(used[i] == 1){
	            continue;
	        }
	        else{
	            r.add(set[i]);
	            used[i] = 1;
	            //count++;
	            combinations(k, set, used);
	            used[i] = 0;
	            r.remove(r.size() - 1);
	            return;
	            //count --;
	        }
	    }
	}

	public static void sub(int[] S){
		 int[] used = new int[S.length];
	        for(int i = 0; i < S.length; i++){
	        used[i] = 0;
	        }
	        for(int i = 2; i <= S.length; i++){
	        combinations(i, S, used);
	        }
	}
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
   if(S.length == 0)
				return null;
			for(int i = 0; i < S.length; i++){
				 ArrayList<Integer> rr = new ArrayList<Integer>();
			        rr.add(S[i]);
			        result.add(rr);
			        sub(Arrays.copyOfRange(S, i, S.length));
			}
			
	        ArrayList<Integer> rr = new ArrayList<Integer>();
	        result.add(rr);
	        
			return result;
    }
}

以下是正确的算法和AC代码:

Thoughts

Given a set S of n distinct integers, there is a relation between Sn and Sn-1. The subset of Sn-1 is the union of {subset of Sn-1} and {each element in Sn-1 + one more element}. Therefore, a Java solution can be quickly formalized.

public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
	if (S == null)
		return null;
 
	Arrays.sort(S);
 
	ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 
	for (int i = 0; i < S.length; i++) {
		ArrayList<ArrayList<Integer>> temp = new ArrayList<ArrayList<Integer>>();
 
		//get sets that are already in result
		for (ArrayList<Integer> a : result) {
			temp.add(new ArrayList<Integer>(a));
		}
 
		//add S[i] to existing sets
		for (ArrayList<Integer> a : temp) {
			a.add(S[i]);
		}
 
		//add S[i] only as a set
		ArrayList<Integer> single = new ArrayList<Integer>();
		single.add(S[i]);
		temp.add(single);
 
		result.addAll(temp);
	}
 
	//add empty set
	result.add(new ArrayList<Integer>());
 
	return result;
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值