Skip to content
RED SAGSkills
medium10 pts · first answer countsdynamic-programmingbacktrackingdebugging

All results come out empty

This permutation code returns a list of empty arrays. Why?

function permute(nums) {
  const out = [], path = [], used = []
  const go = () => {
    if (path.length === nums.length) { out.push(path); return }
    for (let i = 0; i < nums.length; i++) {
      if (used[i]) continue
      used[i] = true; path.push(nums[i])
      go()
      path.pop(); used[i] = false
    }
  }
  go()
  return out
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions