Java实现 LeetCode 90 子集 II(二)
90. 子集 II
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例:
输入: [1,2,2]
输出:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
PS:
先进行排序,保证重复元素挨在一起
记录每次遍历生成的新序列的长度,这里用left表示每次遍历的开始位置,right结束位置,len表示长度
根据与前面元素是否重复,来决定left的取值,也就是开始遍历的位置
其实和子集1还是差不多的
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> retList = new ArrayList<>();
retList.add(new ArrayList<>());
if(nums == null || nums.length == 0) return retList;
Arrays.sort(nums);
List<Integer> tmp = new ArrayList<>();
tmp.add(nums[0]);
retList.add(tmp);
if(nums.length == 1) return retList;
int lastLen = 1;
for(int i = 1; i < nums.length; i++){
int size = retList.size();
if(nums[i] != nums[i-1]){
lastLen = size;
}
for(int j = size - lastLen; j < size; j++){
List<Integer> inner = new ArrayList(retList.get(j));
inner.add(nums[i]);
retList.add(inner);
}
}
return retList;
}
}
Java实现 LeetCode 90 子集 II(二)的更多相关文章
- [leetcode] 90. 子集 II.md
90. 子集 II 78. 子集题的扩展,其中的元素可能会出现重复了 我们仍沿用78题的代码,稍作改动即可: 此时需要对nums先排个序,方便我们后面跳过选取相同的子集. 跳过选取相同的子集.当选取完 ...
- leetcode 90. 子集 II JAVA
题目: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2] ...
- Leetcode 90. 子集 II
地址 https://leetcode-cn.com/problems/subsets-ii/ 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重 ...
- LeetCode 90. 子集 II(Subsets II)
题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2 ...
- LeetCode -90. 子集 II C++ (回溯法)
class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) ...
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
- 90. 子集 II
90. 子集 II 题意 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2]输出:[ [2], [1], ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- CF#633 C. Powered Addition 思维
Powered Addition 题意 给出n个数字,现在你可以在第x秒,选择任意数量的下标,让这些位置上的数加上\(2^{x-1}\),问最快需要几秒使得数列变成一个非递减的序列. 思路 让求x的最 ...
- 业务系统请求zabbix图表性能调优
性能调优实践 性能调优实践 背景 问题分析 后端优化排查 前端优化排查 后端长响应排查 zabbix server 优化 总结 背景 用 vue.js 的框架 ant-design vue pro 实 ...
- 暴力破解-HTTP Basic认证
0x01 HTTP Basic认证介绍 基本认证 basic authentication ← HTTP1.0提出的认证方法 基本认证步骤: 1. 客户端访问一个受http基本认证保护的资源. 2. ...
- 【基准测试】BenchmarkDotNet介绍
BenchmarkDotNet 概述 BenchmarkDotNet helps you to transform methods into benchmarks, track their perfo ...
- call(),apply(),bind() 区别和用法
call call 方法第一个参数是要绑定给this的值,后面传入的是一个参数列表.当第一个参数为null.undefined的时候,默认指向window. var arr = [1, 2, 3, 8 ...
- elasticsearch 小总结
elasticsearch 小总结 0. 起因 距离初次写关于es的文章 https://blog.csdn.net/aca_jingru/article/details/44488703 已经过去4 ...
- 终于明白了vue使用axios发送post请求时的坑及解决原理
前言:在做项目的时候正好同事碰到了这个问题,问为什么用axios在发送请求的时候没有成功,请求不到数据,反而是报错了,下图就是报错请求本尊 vue里代码如下: this.$http.post('/ge ...
- C# 使用Word模板导出数据
使用NPOI控件导出数据到Word模板中方式: 效果如下: Word模板: 运行结果: 实现如下: Student.cs using System; using System.Collections. ...
- MyBatis的使用增删改查(两种分页查询)
文件目录 写一下每个文件的代码 UserDao.java package cn.zys.dao; import java.io.IOException; import java.util.List; ...
- 第3章 衡量线性回归的指标:MSE,RMSE,MAE
, , ,, , , ,