Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates&nbsp;where the candidate numbers sum to target.

Each number in candidates&nbsp;may only be used once in the combination.

Note:&nbsp;The solution set must not contain duplicate combinations.

&nbsp;
Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8
Output: 
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]


Example 2:

Input: candidates = [2,5,2,1,2], target = 5
Output: 
[
[1,2,2],
[5]
]


&nbsp;
Constraints:


	1 &lt;=&nbsp;candidates.length &lt;= 100
	1 &lt;=&nbsp;candidates[i] &lt;= 50
	1 &lt;= target &lt;= 30


class Solution(object):
    def combinationSum2(self, candidates, target):
        res = []
        def dfs(nums,summ,curr):
            if summ>=target:
                if summ == target:
                    res.append(curr)
                return
            for i in range(len(nums)):
                if i !=0 and nums[i]==nums[i-1]:
                    continue
                dfs(nums[i+1:],summ+nums[i],curr+[nums[i]])
        dfs(sorted(candidates),0,[])
        return res
class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        // O(nlogn)
        Arrays.sort(candidates);
        boolean[] visited = new boolean[candidates.length];
        helper(res, path, candidates, visited, target, 0);
        return res;
    }
    private void helper(List<List<Integer>> res,
                  List<Integer> path, int[] candidates,
                  boolean[] visited, int remain, int currIndex
                 ){
        if (remain == 0){
            res.add(new ArrayList<>(path));
            return;
        }
        if (remain < 0){
            return;
        }

        for(int i = currIndex; i < candidates.length; i++){
            if (visited[i]){
                continue;
            }
            if (i > 0 && candidates[i] == candidates[i - 1] && !visited[i - 1]){
                continue;
            }
            int curr = candidates[i];
            path.add(curr);
            visited[i] = true;
            helper(res, path, candidates, visited, remain - curr, i + 1);
            path.remove(path.size() - 1);

            visited[i] = false;
        }
    }
}
class Solution {
public:
    vector<vector<int>> ans;vector<int> temp;
    void f(vector<int>& nums,int target,int i){
        if(target==0){
            ans.push_back(temp);
            return;
        }
        if(i>=nums.size())
            return;
        if(nums[i]<=target){
            temp.push_back(nums[i]);
            f(nums,target-nums[i],i+1);
            temp.pop_back();
            while(i<nums.size()-1 && nums[i]==nums[i+1]) i++;
            f(nums,target,i+1);
        }
        else
            f(nums,target,i+1);
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        sort(candidates.begin(),candidates.end());
        f(candidates,target,0);
        return ans;
    }
};
/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum2 = function(candidates, target) {
    candidates.sort((a, b) => a - b);
    
    const ans = [];
    
    function dfs(idx, t, st) {
        if (t === 0) {
            ans.push(Array.from(st));
            return;
        }
        for (let i = idx; i < candidates.length; i++) {
            if (i > idx && candidates[i - 1] === candidates[i]) continue;
            if (candidates[i] > t) break;
            st.push(candidates[i]);
            dfs(i + 1, t - candidates[i], st);
            st.pop();
        }
    }
    
    dfs(0, target, []);
    
    return ans;
};
Combination Sum II