当前位置:网站首页>Leetcode78. Subset
Leetcode78. Subset
2022-08-08 04:29:00 【Java Full Stack R&D Alliance】
题目传送地址: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]; //the last element of the current array
//处理边界情况
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); //A subset of the previous array is also part of the subset that belongs to the new array
for (int i = 0; i < subsets.size(); i++) {
List<Integer> list1 = new ArrayList<>(subsets.get(i));
list1.add(lastNum);
res.add(list1);
}
return res;
}
}
边栏推荐
- 由联合体union引出的大小端问题
- Mini Program Optimization Practice
- Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk
- MySQL from entry to entry [20W word collection]
- Redis持久化机制、主从、哨兵、cluster集群方案解析
- leetcode: 874. Simulate a walking robot
- Amazon Cloud Technology Build On Learning Experience
- 奇怪的魔法(组合数)
- MySQL - Indexes and Transactions
- 【代码分析】图小样本异常检测方法:GDN:Few-shot Network Anomaly Detection via Cross-network Meta-learning
猜你喜欢

国内最主流的5大项目工时管理系统

leetcode: 874. 模拟行走机器人

走进音视频的世界——RGB与YUV格式

Amazon Cloud Technology Build On Learning Experience

New retail project and offline warehouse core interview,, 220807,,

Mini Program Optimization Practice

unity之粒子特效制作图片拼合文字效果

07查询表达式 及 page分页、order 排序《ThinkPHP6 入门到电商实战》

MySQL从入门到入土【20W字收藏篇】

ES6解构赋值的使用说明
随机推荐
LeetCode_485_最大连续1的个数
Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk
【保研面试】英文问题
08 获取器 withAttr、多连缀、whereRaw、事务、数据集《ThinkPHP6 入门到电商实战》
顺序表(下)
报错:[Intervention] Unable to preventDefault inside passive event listener due to target ...
中间件的一些坑记录
机器学习笔记:学习率预热 warmup
Implementing Express middleware principles
The research project of the Institute of Metal Research of the Chinese Academy of Sciences has been certified by Huawei, helping to develop a new paradigm in materials science!
KMP和EXKMP(Z函数)
【图基础】如何定义异质图上的小样本学习:Heterogeneous Graph Few-Shot Learning
Exercise equipment responsive pbootcms template class web site
Cube - studio deployment process
leetcode 70. Stair Climbing Dynamic Programming
New User Plane Design and Key Technologies in the 6G Era
Open3D ICP精配准(使用鲁棒性核函数)
MindFusion.WPF Pack 2022.R1
【模板引擎】velocity
数据库缓冲区高速缓存中块的类型