【leetcode刷题笔记】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
跟subsets这道题非常相像,除了S中会有重复的元素,那么会有什么影响呢?看一个例子:
S = [1,1]
根据subsets中的算法,生成的子集如下:
[
[],
[1],
[1],
[1,1]
]
显然[1],[1]这两个是重复的,需要去掉。那么我们怎么判重?只需要在递归函数的循环里面加上如下判断
if(i != pos && s[i] == s[i - 1]) continue; 就可以了。
理解这个判断的关键,是搞清楚递归函数
public void subsetsDfs(int[] s,int level,int pos,ArrayList<Integer> result,List<List<Integer>> answer)
中pos变量的含义。
在这个函数中,level表示要生成的子集的长度,pos则表示目前要生成子集的第pos位上的元素。那么当i==pos的时候,说明子集中第pos位上的元素还没有生成过,那么即使s[i]==s[i-1],它们在生成的子集中所占有的位置不同(例如例子中的[1,1]中的两个1分别在生成的子集的第0位和第1位上),s[i]就可以加到这个子集上去。但是当i != pos的时候,说明生成的子集上第pos位已经有元素了,如果当前的s[i]和这个元素s[i-1]相等,那么就没有必要再在这个位置上重复放置s[i]了(例如上述例子中已经有子集[1]的时候,不能够再生成子集[s[1]=1]了)。
最后实现的代码如下:
public class Solution {
public void subsetsDfs(int[] s,int level,int pos,ArrayList<Integer> result,List<List<Integer>> answer){
if(result.size() == level){
answer.add(new ArrayList<Integer>(result));
return;
}
for(int i = pos;i < s.length;i++){
if(i != pos && s[i] == s[i - 1])
continue;
result.add(s[i]);
subsetsDfs(s, level, i+1, result, answer);
result.remove(result.size()-1);
}
}
public List<List<Integer>> subsetsWithDup(int[] S) {
Arrays.sort(S);
List<List<Integer>> answer = new ArrayList<List<Integer>>();
ArrayList<Integer> result = new ArrayList<Integer>();
for(int i = 0;i <= S.length;i++)
subsetsDfs(S, i, 0, result, answer);
return answer;
}
}
【leetcode刷题笔记】Subsets II的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode刷题笔记】Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode刷题笔记】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode刷题笔记】Populating Next Right Pointers in Each Node II
What if the given tree could be any binary tree? Would your previous solution still work? Note: You ...
随机推荐
- 第九章 用多线程来读取epoll模型下的client数据
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include &l ...
- Python学习笔记(一)三步走安装pip
pip是用来方便地管理Python的第三方包的,由于此前玩Python仅仅是浅尝辄止,用的是python(x,y),但是这里并不代表你想用什么包都能从里面找到的,所以我把python(x,y)卸了,然 ...
- Unity3D - 性能优化之Draw Call
Unity3D - 性能优化之Draw Call 分类: Unity 3D2012-09-13 11:18 1002人阅读 评论(0) 收藏 举报 性能优化引擎测试脚本图形算法 Unity(或者说基本 ...
- centos7 firefox 安装flash
在官网下载flash的tar包 https://get.adobe.com/flashplayer/?spm=a2h0j.8191423.movie_player.5~5~5~8~A 在下载tar包的 ...
- scala 遇到过的问题
1:在我安装完scala的插件后,在打开方法的实现类(open implementactions)的时候,抛出这个异常,后来发现这个异常是因为我的scala的插件跟我eclipse版本不兼容导致的. ...
- vi 详解
1.vi的基本概念 基本上vi可以分为三种状态,分别是命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode),各模式的功能区分如下: ...
- PHP性能:序——谈性能
PHP性能:序——谈性能 这里不谈PHP的性能和其他语言的性能,这里讨论PHP自身的性能问题. 性能是什么? 通俗的来讲,性能,就是在固定的环境下能做的事情的多少. 为什么要性能? 1.每一个软件或网 ...
- div+css 画三角形
<style type="text/css"> .rightdirection { width:0;height:0; line-height:0; b ...
- 基于react-native android的新闻app的开发
使用平台:android 代码获取地址:https://github.com/wuwanyu/ReactNative-Android-MovieDemo 项目展示: 结构图: SpalashScree ...
- python 微信跳一跳进阶
上一篇是通过图片识别来计算跳的距离,再计算按压时间,最后通过adb来控制手机跳的 本篇讲的是通过机器学习,来训练的算法进行跳一跳的 链接: github:https://github.com/Prin ...