90. Subsets II (Java)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
result = new ArrayList<>();
List<Integer> ans = new ArrayList<Integer>();
Arrays.sort(nums); //nums可能是乱序的,要先排序
backtrack(ans, nums, 0);
return result;
} public void backtrack(List<Integer> ans, int[] nums, int depth){
if(depth >= nums.length) {
List<Integer> new_ans = new ArrayList<Integer>(ans);
result.add(new_ans);
return;
} int i = depth+1;
while(i < nums.length){
if(nums[depth] == nums[i]) i++;
else break;
} int j = depth;
backtrack(ans, nums, i); //not add
while(j < i){
ans.add(nums[depth]);
backtrack(ans, nums, i);
j++;
} //reset
while(j > depth){
ans.remove(ans.size()-1);
j--;
}
} private List<List<Integer>> result;
}
90. Subsets II (Java)的更多相关文章
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- 78. Subsets 90. Subsets II
1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- 90. Subsets II (Back-Track, DP)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
随机推荐
- [Java]用于将链表变成字符串并在元素之间插入分隔符的有用函数“String.join”
将链表变成字符串并在元素之间插入分隔符,这种动作最常见于组合sql文“select a,b,c from tb”这种场景scenario,其中a,b,c你是存贮在链表中的, 如果要加逗号要么在循环中识 ...
- Cortex-M3 咬尾中断 与 晚到中断
[咬尾中断]在处理器在响应某些异常时,如果又发生其他异常,但它们优先级不够高,则它们会被阻塞. 那么,在当前的异常执行返回后,系统处理悬起的异常时,倘若还是先POP,然后又把POP处理的内容PUSH回 ...
- LC 846. Hand of Straights
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into ...
- Python:百科
ylbtech-Python:百科 Python是一种跨平台的计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越 ...
- 阶段3 3.SpringMVC·_06.异常处理及拦截器_4 SpringMVC拦截器之介绍和搭建环境
拦截器可以有多个 搭建环境 不用改,直接finish 复制原来项目的 依赖的包也复制过来 web.xml配置前端控制器 springmvc的配置文件 先创建对应的文件夹 分别创建java和resour ...
- 关于web技术的一些见解
在目前的软件技术领域中,互联网方面的技术是其中最热门的一部分.现在做一个普通的网站,就涉及到大部分的web技术了:前端展示,后端数据处理,功能模块等.我觉得,也就分两个部分的技术:前端,后端. 前端, ...
- 基于nodeJS的小说爬虫实战
背景与需求分析 最近迷恋于王者荣耀.斗鱼直播与B站吃播视频,中毒太深,下班之后无心看书. 为了摆脱现状,能习惯看书,我开始看小说了,然而小说网站广告多而烦,屌丝心态不愿充钱,于是想到了爬虫. 功能分析 ...
- 业务型代码常用的SQL汇总(随时更新)
做了一年的业务代码开发,记录并分享一下自己平时在项目中遇到的比较好用的sql 1.查询表中是否某一字段下的数据有重复数据(以ID为例) SELECT id FROM 表名GROUP BY ID HAV ...
- 对JavaScript事件处理程序/事件监听器的设定的简单介绍
下面是一些对事件处理进行设定的方式. 指定为HTML元素的属性(事件处理程序) 指定为DOM元素的属性(事件处理程序) 通过EventTarget.addEventListener()进行指定(事件监 ...
- SQL Injection(Blind)
SQL Injection(Blind),即SQL盲注,与一般注入的区别在于,一般的注入攻击者可以直接从页面上看到注入语句的执行结果,而盲注时攻击者通常是无法从显示页面上获取执行结果,甚至连注入语句是 ...