← Back

Backtracking (Subsets)

The choose / explore / un-choose template behind subsets, permutations, and combinations.

1 / 1
func subsets(nums []int) [][]int {

}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path = path[:len(path)-1]
        }
    }
}
func subsets(nums []int) [][]int {
    // res collects finished subsets; path is the one we're currently building.
    var res [][]int
    var path []int

    // backtrack explores every subset that draws from index `start` onward.
    var backtrack func(start int)
    backtrack = func(start int) {
        // Every node of the recursion is itself a valid subset. Store a COPY,
        // because path keeps mutating under us.
        res = append(res, append([]int{}, path...))

        // Try each remaining element as the next one to include.
        for i := start; i < len(nums); i++ {
            // CHOOSE: add nums[i] to the current subset.
            path = append(path, nums[i])

            // EXPLORE: recurse on elements after i (i+1 avoids reusing/reordering).
            backtrack(i + 1)

            // UN-CHOOSE: remove nums[i] so the next iteration starts clean.
            path = path[:len(path)-1]
        }
    }

    backtrack(0) // kick off from the empty subset
    return res
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.