From b6b73f20a541cb20ec12373b25cacd1fe495745b Mon Sep 17 00:00:00 2001 From: callmePicacho <38685653+callmePicacho@users.noreply.github.com> Date: Thu, 8 Jul 2021 11:24:14 +0800 Subject: [PATCH] =?UTF-8?q?Update=200047.=E5=85=A8=E6=8E=92=E5=88=97II.md?= =?UTF-8?q?=20Go=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0047.全排列II.md | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index 079ca834..b6c12919 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -5,6 +5,7 @@

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 排列问题(二) ## 47.全排列 II @@ -222,6 +223,43 @@ class Solution: return res ``` +Go: + +```go +var res [][]int +func permute(nums []int) [][]int { + res = [][]int{} + sort.Ints(nums) + dfs(nums, make([]int, 0), make([]bool, len(nums))) + return res +} + +func dfs(nums, path []int, used []bool) { + if len(path) == len(nums) { + res = append(res, append([]int{}, path...)) + return + } + + m := make(map[int]bool) + for i := 0; i < len(nums); i++ { + // used 从剩余 nums 中选 + if used[i] { + continue + } + // m 集合间去重 + if _, ok := m[nums[i]]; ok { + continue + } + m[nums[i]] = true + path = append(path, nums[i]) + used[i] = true + dfs(nums, path, used) + used[i] = false + path = path[:len(path)-1] + } +} +``` + Javascript: ```javascript