Review: Combinations and Permutations Problems

Combinations and Permutations Problems

Sample Problems:

Code Skeleton:

func dfs(combination []int, pos int, nums []int, res *[][]int) {
     // No need to keep going
     if (...) {
       *res = append(*res, combination)

     // Add nums[pos]
     for i:=pos; i<len(nums); i++ {
        // deep copy
        combination2 := make([]int, len(combination))
        copy(combination2, combination)
        combination2 = append(combination2, nums[i])
        dfs(combination2, i+1, nums, res)

// main function

See all combination: #combination

See more blog_posts.


Share It, If You Like It.

Leave a Reply

Your email address will not be published.