Wonderland
  • README.md
  • Notebook
    • Crypto
      • Solana
        • Troubleshooting
          • BPF SDK path does not exist
    • Language
      • Rust
        • Reference
          • Capturing the Environment with Closures
          • Understanding &mut &mut Reference
      • C++
        • Reference
          • Code for MS rand() and srand() in VC++ 6.0
  • Textbook
    • Serials
      • A Real-Time Cryptocurrency Ticker Dashboard
        • 0 - Some Preparation
    • Frontend
      • A Simple Progress Bar
      • A React Ribbon Component
      • An Easy to Use React DnD Sortable Component
      • Sticky Header, Sticky Footer and Fluid Content
      • How To Set Same Height As Width In CSS
  • dictionary
    • Alphabet
      • MySQL
      • FFmpeg
    • Algorithm
      • Diary
        • 2022
          • 07
            • 2022-07-02
            • 2022-07-01
          • 06
            • 2022-06-30
            • 2022-06-29
            • 2022-06-28
            • 2022-06-27
            • 2022-06-26
            • 2022-06-25
            • 2022-06-24
            • 2022-06-23
            • 2022-06-22
            • 2022-06-21
            • 2022-06-20
            • 2022-06-19
            • 2022-06-18
            • 2022-06-17
            • 2022-06-16
            • 2022-06-15
            • 2022-06-14
            • 2022-06-13
            • 2022-06-12
            • 2022-06-11
            • 2022-06-10
            • 2022-06-09
            • 2022-06-08
            • 2022-06-07
            • 2022-06-06
            • 2022-06-05
            • 2022-06-04
            • 2022-06-03
            • 2022-06-02
            • 2022-06-01
          • 05
            • 2022-05-31
            • 2022-05-30
            • 2022-05-29
            • 2022-05-28
            • 2022-05-27
            • 2022-05-26
            • 2022-05-25
            • 2022-05-24
            • 2022-05-23
            • 2022-05-22
            • 2022-05-21
            • 2022-05-20
            • 2022-05-19
            • 2022-05-18
            • 2022-05-17
            • 2022-05-16
            • 2022-05-15
    • Troubleshooting
      • A Weird Python Command Not Found Problem
Powered by GitBook
On this page
  • 47. Permutations II
  • Description
  • Solution
  • 39. Combination Sum
  • Description
  • Solution
  • 40. Combination Sum II
  • Description
  • Solution
  1. dictionary
  2. Algorithm
  3. Diary
  4. 2022
  5. 06

2022-06-06

Last updated 2 years ago

Description

Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.

Example 1:

Input: nums = [1,1,2]
Output:
[[1,1,2],
 [1,2,1],
 [2,1,1]]

Example 2:

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Constraints:

  • 1 <= nums.length <= 8

  • -10 <= nums[i] <= 10

Solution

Approach #0

func permuteUnique(nums []int) (ans [][]int) {
    sort.Ints(nums)
    n := len(nums)
    vis := make([]bool, len(nums))
    var tmp []int
    var backtrack func(int)
    backtrack = func(cur int) {
        if cur == n {
            ans = append(ans, append([]int(nil), tmp...))
            return
        }
        for i, num := range nums {
            if vis[i] || i > 0 && !vis[i-1] && nums[i-1] == num {
                continue
            }
            vis[i] = true
            tmp = append(tmp, num)
            backtrack(cur + 1)
            tmp = tmp[:len(tmp)-1]
            vis[i] = false
        }
    }
    backtrack(0)
    return
}

Description

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:

Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.

Example 2:

Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]

Example 3:

Input: candidates = [2], target = 1
Output: []

Constraints:

  • 1 <= candidates.length <= 30

  • 1 <= candidates[i] <= 200

  • All elements of candidates are distinct.

  • 1 <= target <= 500

Solution

Approach #0

func combinationSum(candidates []int, target int) (ans [][]int) {
    sort.Ints(candidates)
    n := len(candidates)
    var t []int
    var backtrack func(cur, index int)
    backtrack = func(cur, index int) {
        if cur == target {
            ans = append(ans, append([]int(nil), t...))
            return
        }
        if cur > target {
            return
        }
        for i := index; i < n; i++ {
            t = append(t, candidates[i])
            backtrack(cur+candidates[i], i)
            t = t[:len(t)-1]
        }
    }
    backtrack(0, 0)
    return
}

Description

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

Each number in candidates may only be used once in the combination.

Note: The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8
Output: 
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

Example 2:

Input: candidates = [2,5,2,1,2], target = 5
Output: 
[
[1,2,2],
[5]
]

Constraints:

  • 1 <= candidates.length <= 100

  • 1 <= candidates[i] <= 50

  • 1 <= target <= 30

Solution

Approach #0

func combinationSum2(candidates []int, target int) (ans [][]int) {
    sort.Ints(candidates)
    n := len(candidates)
    vis := make([]bool, n)
    var t []int
    var backtrack func(index, cur int)
    backtrack = func(index, cur int) {
        if cur == target {
            ans = append(ans, append([]int(nil), t...))
            return
        }
        if cur > target {
            return
        }
        for i := index; i < n; i++ {
            if i > 0 && !vis[i-1] && candidates[i-1] == candidates[i] {
                continue
            }
            t = append(t, candidates[i])
            vis[i] = true
            backtrack(i+1, cur+candidates[i])
            vis[i] = false
            t = t[:len(t)-1]
        }
    }
    backtrack(0, 0)
    return
}

47. Permutations II
39. Combination Sum
40. Combination Sum II