My java accept solution


  • 0
    D
    public class Solution {
    	private List<List<Integer>> result;
    	private List<Integer> element;
    	public List<List<Integer>> combinationSum2(int[] num, int target) {
            result = new ArrayList<List<Integer>>();
    //        element = new ArrayList<Integer>();
            Arrays.sort(num);
            if(num[0]> target)
            	return result;
            for(int i=0;i<num.length;i++) {
            	element = new ArrayList<Integer>();
            	if(num[i] < target) {
            		element.add(num[i]);
            		helper(num, i, target-num[i]);
            	}
            	else if(num[i] == target) {
            		element.add(num[i]);
            		result.add(element);
            		break;
            	}
            }
            
            result = removeDuplicate(result);
            return result;
        }
    	
    	public static List<List<Integer>> removeDuplicate(List<List<Integer>> list) {
    		HashSet<List<Integer>> hashset = new HashSet<List<Integer>>(list);
    		list.clear();
    		list.addAll(hashset);
    		return list;
    	}
    	
    	public void helper(int[] can, int index, int t) {
    		for(int i=index+1;i<can.length;i++) {
    			if(can[i]<t) {
    				element.add(can[i]);
    				helper(can, i, t-can[i]);
    				
    				element.remove(element.size()-1);
    			}else if(can[i] == t) {
    				element.add(can[i]);
    				result.add(element);
    				element = new ArrayList<Integer>(element);
    				element.remove(element.size()-1);
    				break;
    			}else {
    				break;
    			}
    		}
    	}
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.