permutations_ii

  • 2022-12-14
  • 浏览 (534)

permutations_ii.go 源码

package main

import "sort"

//全排列II

func permuteUnique(nums []int) [][]int {
	sort.Ints(nums)
	var res [][]int
	var data []int
	visited := make([]bool, len(nums))
	permuteUniqueHelper(nums, visited, data, &res)
	return res
}

func permuteUniqueHelper(nums []int, visited []bool, data []int, res *[][]int) {
	if len(data) == len(nums) {
		temp := make([]int, len(nums))
		copy(temp, data)
		*res = append(*res, temp)
		return
	}

	for i, v := range nums {
		if (i > 0 && nums[i] == nums[i-1] && !visited[i-1]) || visited[i] {
			continue
		}
		data = append(data, v)
		visited[i] = true
		permuteUniqueHelper(nums, visited, data, res)
		visited[i] = false
		data = data[:len(data)-1]
	}
}

你可能感兴趣的文章

bst_lowest_commona_ancestor

combinations

generate_parentheses

0  赞