Combination Sum II LeetCode solution in JavaScript
1/**2* @param {number[]} candidates3* @param {number} target4* @return {number[][]}5*/6var combinationSum2 = function (candidates, target) {7candidates.sort((a, b) => a - b);89const ans = [];1011function dfs(idx, t, st) {12if (t === 0) {13ans.push(Array.from(st));14return;15}16for (let i = idx; i < candidates.length; i++) {17if (i > idx && candidates[i - 1] === candidates[i]) continue;18if (candidates[i] > t) break;19st.push(candidates[i]);20dfs(i + 1, t - candidates[i], st);21st.pop();22}23}2425dfs(0, target, []);2627return ans;28};