LeetCode 78.子集 - Go 实现
2023-06-13 09:15:25 时间
- 子集
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例 1:
输入:nums = [1,2,3] 输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
示例 2:
输入:nums = [0] 输出:[[],[0]]
AC 代码
func subsets(nums []int) [][]int {
l := list.New()
result := list.New()
for i := 0; i <= len(nums); i++ {
dfs(nums, 0, i, l, result)
}
arr := make([][]int, result.Len())
k := 0
for e := result.Front(); e != nil; e = e.Next(){
curl := e.Value.(*list.List)
arr[k] = make([]int, curl.Len())
k++
}
i := 0;
for e := result.Front(); e != nil; e = e.Next() {
//fmt.Println(e.Value.(*list.List).Front().Value)
curl := e.Value.(*list.List)
j := 0
for p := curl.Front(); p != nil; p = p.Next() {
//fmt.Println(p.Value)
arr[i][j] = p.Value.(int)
j++
}
i++;
}
return arr
}
func dfs(nums []int, start int, len int, l *list.List, result *list.List) {
if start == len {
a := list.New()
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value.(int))
a.PushBack(e.Value)
}
result.PushBack(a)
return
}
for i := start; i < len; i++ {
l.PushBack(nums[i])
dfs(nums, i+1, len, l, result)
b := l.Back()
l.Remove(b)
}
}
参考资料
- 子集
相关文章
- LeetCode每日一题-4:合并两个有序链表
- LeetCode每日一题-6:k个一组翻转链表
- leetcode之 两数之和 题目解答C/python
- Array相关LeetCode题目笔记
- leetcode-167两数之和(双指针+二分)
- Leetcode分类——贪心算法
- Leetcode分类——递归、回溯、分治
- 前端工程师leetcode算法面试必备-简单的二叉树
- leetcode刷题(125)——931. 下降路径最小和
- 用Js怒刷LeetCode_2023-02-27
- Leetcode 11. 盛最多水的容器
- LeetCode-分治
- 「Go工具箱」go语言csrf库的使用方式和实现原理
- Go-包管理-go build(二)
- Go语言go mod包依赖管理工具使用详解
- go test命令(Go语言测试命令)完全攻略
- 使用Go语言来操作Oracle数据库(go语言oracle)
- Linux下安装Go环境指南(linux安装go环境)
- 环境Go 编译 在 Linux 环境中的体验(go编译linux)
- 深度探索Go语言与Redis结合(go-redis)
- Go语言环境配置:在 Linux 下实现(go环境搭建linux)
- SQL Server快速上手GO!(sqlserver go)
- 徒手用Go编写Redis迈向新技术世界的旅程(徒手用go写个redis)
- MySQL中使用Go语言的好处(mysql中go什么)
- Go语句让Oracle数据库管理更方便(oracle go语句)