当前位置:网站首页>Leetcode78. 子集
Leetcode78. 子集
2022-08-08 04:23:00 【Java全栈研发大联盟】
题目传送地址:https://leetcode.cn/problems/subsets/
运行效率:
代码如下 递归解法
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
int lastNum = nums[nums.length - 1]; //当前数组最后一个元素
//处理边界情况
if(nums.length==1){
List<Integer> list = new ArrayList<>();
list.add(lastNum);
res.add(Arrays.asList());
res.add(list);
return res;
}
int[] subArray = Arrays.copyOf(nums, nums.length - 1);
List<List<Integer>> subsets = subsets(subArray);
res.addAll(subsets); //上一个数组的子集也是属于新数组的子集的一部分
for (int i = 0; i < subsets.size(); i++) {
List<Integer> list1 = new ArrayList<>(subsets.get(i));
list1.add(lastNum);
res.add(list1);
}
return res;
}
}
边栏推荐
- MySQL - Indexes and Transactions
- CARLA 笔记(05)— Actors and blueprints(创建和修改 Blueprint、生成 Spawning、使用 Handling、销毁 Destruction)
- 一文带你彻底了解synchronized 和 Lock
- 牛客多校第6场赛后学习 B(两种做法)G(两种做法)M(两种写法)J
- topk()/eq( ) / gt( ) / lt( ) / t( )的用法
- MySQL from entry to entry [20W word collection]
- [Graph Basics] How to Define Few-Shot Learning on Heterogeneous Graphs: Heterogeneous Graph Few-Shot Learning
- 32. Do you know how Redis strings are implemented?
- Add OnMouseMove MFC dialog box
- torch.view()函数用法
猜你喜欢

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

【Review of Live Streaming】Synthesis MindSpore Usability SIG2022 First Half Review Summary

NetCore uses Dapper to query data

中国科学院金属研究所科研课题获华为技术认证,助力材料学发展新范式!

The fledgling Xiao Li's 115th blog project notes on the creation of the domestic GD32F103RCT6 basic project

Build a personal network disk using z-file and Qiniu cloud object storage

06 tp6 的数据更新(改)及删除 《ThinkPHP6 入门到电商实战》

新零售项目及离线数仓核心面试,,220807,,

leetcode: 122. 买卖股票的最佳时机 II

Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model
随机推荐
How to avoid bugs as much as possible
【opencv】opencv开发包简介
Qt 日志模块的个性化使用
Shell 脚本 — 多行注释、开启子/不开启子进程执行、转义带颜色输出、读取键盘输入、输入输出重定向、单双引号、命令替换、读取变量、系统变量、正则过滤、算术运算、一行多条命令、字符串比较
Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk
The fledgling Xiao Li's 115th blog project notes on the creation of the domestic GD32F103RCT6 basic project
Usage of topk()/eq( ) / gt( ) / lt( ) / t( )
VSCode打开 C(嵌入式) 工程的一些记录
The type of block in the database buffer cache
【直播回顾】昇思MindSpore易用性SIG2022上半年回顾总结
How to play knowledge graph in recommender system
ES6解构赋值的使用说明
MindFusion.WPF Pack 2022.R1
[opencv] Introduction to opencv development kit
中国科学院金属研究所科研课题获华为技术认证,助力材料学发展新范式!
07查询表达式 及 page分页、order 排序《ThinkPHP6 入门到电商实战》
Bluetooth att gatt agreement
A line of code counts the number of occurrences of the specified string in the text
【多任务模型】《Multi-Faceted Hierarchical Multi-Task Learning for a Large Number of Tasks with Multi-dimens
sessionStorage在不同页签中的数据是否共享问题及解决思路