Leetcode 90. Subsets II 子集2 解题报告
来源:互联网 发布:首趣优店可开淘宝店吗 编辑:程序博客网 时间:2024/04/27 19:25
1 解题思想
这道题有一个简化版,没有看的可以先看看
Leetcode 78. Subsets 子集 解题报告
这道题更复杂的原因在于,给定的数组会存在重复的情况,实际的解法也很common,我相信我已经在之前很多的解题里面说过了,背下来就好:
0、首先需要排序,然后用回溯的方式组合选择,序列在递归到最后一个位置时生成。
规则0:所有数都有不选的这种状况
规则1:第一个数(index=0)可以选
规则2:在任何情况下,当一个数(位置index)和前一个数(index-1)的数值不相同时,可以选中,也可以不选中
规则3:任何情况下,当一个数(位置index)和前一个数(index-1)的数值相同时,当且仅当index-1的数字被选中了,index才能被选中,否则index不能选中
2 原题
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], []]Subscribe to see which companies asked this question
3 AC 解
public class Solution { boolean flag[] ; List<List<Integer>> result; public void search(int[] nums,int index){ if(index == nums.length){ List<Integer> list = new ArrayList<Integer>(); for(int i=0;i<index;i++){ if(flag[i]==true){ list.add(nums[i]); } } result.add(list); return; } search(nums,index+1); if(index==0 || nums[index-1]!=nums[index] || flag[index-1]==true){ flag[index]=true; search(nums,index+1); flag[index]=false; } } public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); this.flag = new boolean[nums.length]; result = new ArrayList<List<Integer>>(); search(nums,0); return result; }}
0 0
- Leetcode 90. Subsets II 子集2 解题报告
- [leetcode] 90. Subsets II 解题报告
- [Leetcode] 90. Subsets II 解题报告
- 【LeetCode】Subsets II 解题报告
- [LeetCode] Subsets II 解题报告
- Leetcode 78. Subsets 子集 解题报告
- leetcode解题之 77. Combinations&78. Subsets&90. Subsets II java 版(求所有子集)
- [LeetCode] Subsets II 子集
- leetCode 90.Subsets II(子集II) 解题思路和方法
- LeetCode 90. Subsets II(子集)
- 【LeetCode】Subsets 解题报告
- LeetCode-Subsets-解题报告
- [LeetCode] Subsets 解题报告
- Leetcode 78 Subsets + 90 Subsets II 子集
- leetcode-90. Subsets II(重复元素子集合)
- 90. Subsets II 子集列举
- [leetcode] 78. Subsets 解题报告
- LeetCode 78. Subsets 解题报告
- PC病毒分析师所需技能和面试题
- nagios监控redis内存使用情况脚本--shell实现
- Yii2.0url美化,去掉r=site/index
- vive 手柄的Grab Mechanics
- 给你个基类如何添加到自己运用程序中
- Leetcode 90. Subsets II 子集2 解题报告
- 安卓之design系列(二)
- Deep Learning(深度学习)学习笔记整理系列
- Map遍历方式及特点分析
- java关键字
- Java 语言中 Enum 类型的使用介绍
- 1157. The hardest problem
- python系列0
- USACO 2015 February CONTEST,Silver&Gold Division Solution