题解 | #没有重复项数字的全排列#

没有重复项数字的全排列

http://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1

package main

/** *

  • @param num int整型一维数组
  • @return int整型二维数组 */ var res [][]int func permute( nums []int ) [][]int { // write code here res = [][]int{} path := []int{} used := make([]bool, len(nums)) backpack(nums, path, used) return res } func backpack(nums []int, path []int, used []bool) { if len(path) == len(nums) { temp := make([]int, len(path)) copy(temp, path) res = append(res, temp) return } for i := 0; i < len(nums); i++ { if !used[i] { used[i] = true path = append(path, nums[i]) backpack(nums, path, used) path = path[:len(path) - 1] used[i] = false } } }
全部评论

相关推荐

totoroyyw:千年老妖😂
投递华为等公司10个岗位
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务