[Leetcode 90]求含有重复数的子集 Subset II
【题目】
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.
【思路】
注意sort,使得判断临接元素是否相邻。
与leetcode78类似,多了一个重复数判断条件
if(i>flag&&nums[i-1]==nums[i])
continue;
【相关题目】
1、[Leetcode 78]求子集 Subset https://www.cnblogs.com/inku/p/9976049.html
2、[Leetcode 90]求含有重复数的子集 Subset II https://www.cnblogs.com/inku/p/9976099.html
3、讲解在这: [Leetcode 216]求给定和的数集合 Combination Sum III
【代码】
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> ans=new ArrayList<>();
List<Integer> tmp=new ArrayList<>();
Arrays.sort(nums);
fun(nums,ans,tmp,0);
return ans;
}
public void fun(int nums[],List<List<Integer>> ans,List<Integer> tmp,int flag){
ans.add(new ArrayList<>(tmp));
for(int i=flag;i<nums.length;i++){
if(i>flag&&nums[i-1]==nums[i])
continue;
tmp.add(nums[i]);
fun(nums,ans,tmp,i+1);
tmp.remove(tmp.size()-1);
}
}
}
[Leetcode 90]求含有重复数的子集 Subset II的更多相关文章
- [Leetcode] subsets 求数组所有的子集
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- [Leetcode 78]求子集 Subset
[题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- 子集系列(一) 传统subset 问题,例 [LeetCode] Subset, Subset II, Bloomberg 的一道面试题
引言 Coding 问题中有时会出现这样的问题:给定一个集合,求出这个集合所有的子集(所谓子集,就是包含原集合中的一部分元素的集合). 或者求出满足一定要求的子集,比如子集中元素总和为定值,子集元素个 ...
- Codeforces Round #467 (Div. 2) A. Olympiad[输入一组数,求该数列合法的子集个数]
A. Olympiad time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Leetcode之回溯法专题-78. 子集(Subsets)
Leetcode之回溯法专题-78. 子集(Subsets) 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = ...
- Java 求集合的所有子集
递归方法调用,求解集合的所有子集. package ch01; import java.util.HashSet; import java.util.Iterator; import java.uti ...
- python 实现求一个集合的子集
概要 今天偶然看到有个关于数学中集合的问题,就突发奇想的想用python实现下求一个集合的子集. 准备 我当然先要复习下,什么是集合,什么是子集? 比较粗犷的讲法,集合就是一堆确定的东西,细致一点的讲 ...
- 傻瓜方法求集合的全部子集问题(java版)
给定随意长度的一个集合.用一个数组表示,如{"a", "b","c"},求它的全部子集.结果是{ {a}, {b}, {c}, {a,b}, ...
随机推荐
- C语言进阶之路(二)----字符串操作常见模型
1.while模型 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #includ ...
- Painter's Problem (高斯消元)
There is a square wall which is made of n*n small square bricks. Some bricks are white while some br ...
- vue store存储commit和dispatch
vue store存储commit和dispatch this.$store.commit('toShowLoginDialog', true);this.$store.dispatch('toSho ...
- 笔记02 linux的一些命令sed
#!/bin/bash # dataformat=`date +%Y-%m-%d-%H-%M` #进行文件件cp并重命名 nginx_home=/opt/modules/nginx-1.12/ cp ...
- 用javaScript对页面元素进行显示和隐藏
将显示元素进行隐藏 用document.getElementById("ID名").hidden=ture;根据页面元素ID名获得页面元素值,进而将其属性设置成隐藏. 将隐藏元素进 ...
- oracle(2)
create table aaa( id number, name varchar2(100) ); select decode((select max(id) from aaa),null,'x', ...
- Linux笔记 #09# Tomcat多开以及Nginx负载均衡简单例子
索引 Tomcat怎样多开.. 1.添加环境变量(最基础.关键的步骤!) 2.改catalina.sh 3.改相关端口 Nginx负载均衡简单例子 Tomcat怎样多开.. 演示一下如何开两个(开n个 ...
- windbg无故不显示command窗口
原文最早发表于百度空间2010-02-05 有的dump可以显示,有的不行……上网找了一通没有收获,自己搞了一下,终于在点击“window”——“cascade floating windows”后出 ...
- 极路由hc5661安装tcpdump
原先有个tcpdump的插件,但是现在已经下架了. 条件: 已root的极1s HC5661 - 1.4.11.21001s 步骤: ssh进去后,opkg install http://downlo ...
- postman(八):使用newman来执行postman脚本
通过之前的了解,我们知道postman是基于javascript语言编写的,而导出的json格式的postman脚本也无法直接在服务器运行,它需要在newman中执行(可以把newman看做postm ...