code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

03 Subsets

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const subsets = nums => {
  const result = []
  const backtrack = (arr, curr) => {
    result.push(curr.slice())
    for (let i = 0; i < arr.length; i++) {
      curr.push(arr[i])
      backtrack(arr.slice(i + 1), curr)
      curr.pop()
    }
  }
  backtrack(nums, [])
  return result
}

const subsets1 = nums => {
  const result = []
  const backtrack = (index, combination) => {
    if (index == nums.length) {
      result.push(combination)
      return
    }
    backtrack(index + 1, [...combination])
    backtrack(index + 1, [...combination, nums[index]])
  }
  backtrack(0, '')
  return result
}

console.log(subsets([1, 2, 3]))

Created 2022-02-25T01:16:46+00:00, updated 2022-02-25T17:38:19+00:00 · History · Edit