subsets-ii(需要思考,包括了子数组的求法)
还是有一定难度的。
基本方法,就是用队列,然后不断累加新的数。这是为了不重复而量身定制的。
如果运行重复,是有更简单清晰的方法,就是每次增加考虑一个数字,然后加到本来每一个结果的后面。如下:
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
res.add(new ArrayList<>());
for (int num: nums) {
List<List<Integer>> resDup = new ArrayList<>(res);
for (List<Integer> list:resDup) {
List<Integer> tmpList = new ArrayList<>(list);
list.add(num);
res.add(tmpList);
}
}
return res;
}
}
针对这道题目的解法:
https://leetcode.com/problems/subsets-ii/
// 好像跟之前也用的类似的方法
package com.company;
import java.util.*;
class Solution {
class Pos {
int pos;
int len;
Pos(int pos, int len) {
this.pos = pos;
this.len = len;
}
}
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
Queue<List<Integer>> qe = new ArrayDeque();
Map<Integer, Pos> mp = new HashMap();
List<List<Integer>> ret = new ArrayList<>();
int len = 0;
for (int i=0; i<nums.length; i++) {
if (i == 0 || nums[i] == nums[i-1]) {
len++;
}
else {
Pos pos = new Pos(i-len, len);
mp.put(nums[i-1], pos);
len = 1;
}
}
Pos pos = new Pos(nums.length-len, len);
mp.put(nums[nums.length-1], pos);
List<Integer> lst = new ArrayList();
qe.offer(lst);
ret.add(lst);
while (!qe.isEmpty()) {
List<Integer> tmpLst = qe.poll();
boolean empty = true;
int lastInt = 0;
int curSize = -1;
int curTail = nums[0];
if (!tmpLst.isEmpty()) {
empty = false;
lastInt = tmpLst.get(tmpLst.size() - 1);
curSize = tmpLst.size() - tmpLst.indexOf(lastInt);
curTail = lastInt;
}
while (true) {
Pos tmpPos = mp.get(curTail);
if (empty || curTail > lastInt || tmpPos.len > curSize) {
List<Integer> inputLst = new ArrayList<>(tmpLst);
inputLst.add(curTail);
qe.offer(inputLst);
ret.add(inputLst);
}
if (tmpPos.pos + tmpPos.len >= nums.length) {
break;
}
curTail = nums[tmpPos.pos + tmpPos.len];
}
}
return ret;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello!");
Solution solution = new Solution();
int[] nums = {1,1,2,2,2};
List<List<Integer>> ret = solution.subsetsWithDup(nums);
System.out.printf("Get ret: %d\n", ret.size());
Iterator<List<Integer>> iter = ret.iterator();
while (iter.hasNext()) {
Iterator itemItr = iter.next().iterator();
while (itemItr.hasNext()) {
System.out.printf("%d,", itemItr.next());
}
System.out.println();
}
System.out.println();
}
}
// 这是之前的方法
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
vector<int> tmp;
result.push_back(tmp);
int vlen;
int is_dup = 0;
vector<int> newtmp;
int nlen = nums.size();
for (int i = 0; i < nlen; i++) {
if (i > 0 && nums[i] == nums[i-1]) {
is_dup++;
}
else {
is_dup = 0;
}
vlen = result.size();
for (int j = 0; j < vlen; j++) {
tmp = result[j];
if (is_dup > 0 && \
(tmp.size() < is_dup || tmp[tmp.size()-is_dup] != nums[i])) {
// ignore dup
continue;
}
newtmp.resize(tmp.size());
copy(tmp.begin(), tmp.end(), newtmp.begin());
newtmp.push_back(nums[i]);
result.push_back(newtmp);
}
}
return result;
}
};
subsets-ii(需要思考,包括了子数组的求法)的更多相关文章
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- 求数组的子数组之和的最大值II
这次在求数组的子数组之和的最大值的条件下又增加了新的约束: 1.要求数组从文件读取. 2.如果输入的数组很大, 并且有很多大的数字, 就会产生比较大的结果 (考虑一下数的溢出), 请保 ...
- lintcode循环数组之连续子数组求和
v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...
- C#中求数组的子数组之和的最大值
<编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...
- lintcode:子数组之和为0
题目: 子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3]. 解 ...
- 最大连续子数组问题2-homework-02
1) 一维数组最大连续子数组 如第homework-01就是一维数组的最大子数组,而当其首位相接时,只需多考虑子数组穿过相接的那个数就行了! 2)二维数组 算法应该和第一次的相似,或者说是将二维转化为 ...
- 【剑指offer】连续子数组的最大和
个開始,到第3个为止).你会不会被他忽悠住? 输入: 输入有多组数据,每组測试数据包括两行. 第一行为一个整数n(0<=n<=100000),当n=0时,输入结束.接下去的一行包括n个整数 ...
- 将数组分割为几个等长度的子数组(使用slice)
先了解一下slice方法: slice() 1.定义:slice()可从已有数组中截取返回指定的元素,形成一个新的数组: 语法:arrayObject.slice(start,end): 参数 描述 ...
随机推荐
- 你用什么方法检查 PHP 脚本的执行效率(通常是脚本执行时间)和数据库 SQL 的效率(通常是数据库 Query 时间), 并定位和分析脚本执行和数据库查询的瓶颈所在?
php: 一般是在你要检查的代码开头记录一个时间,结尾记录一个时间.取差值, 数据库SQL的效率 sql的explain(mysql),启用slow query log记录慢查询. 通常还要 ...
- c#之委托总结
1.委托的声明和编译原理 声明委托: delegate void Translate(string str); 通过反编译之后可得下面代码 private sealed class Translate ...
- Interview-Largest independent set in binary tree.
BT(binary tree), want to find the LIS(largest independent set) of the BT. LIS: if the current node i ...
- Team Homework #1 学长“学霸英语学习软件”试用
简介: 一款英语单词记忆和管理辅助软件. 基本功能: 内置GRE词汇及其常考形态.Webster英语解释 单词发音功能 单词测验模式 简易词典功能 基本界面 词库单词读取 单词测试 优点: 1.界面简 ...
- iOS关于CGContextSetBlendMode: invalid context 0x0的错误
在ios 7的模拟器中,选择一个输入框准备输入时,会触发这个错误,以下是出错详细日志: <Error>: CGContextSetBlendMode: invalid context 0x ...
- 常见的装置与其在Linux当中的档名
需要特别留意的是硬盘机(不论是IDE/SCSI/U盘都一样),每个磁碟机的磁盘分区(partition)不同时, 其磁碟档名还会改变呢!下一小节我们会介绍磁盘分区的相关概念啦!需要特别注意的是磁带机的 ...
- ffmpeg 打开视频流太慢(下)
前面的博文中已经交代过,ffmpeg打开视频慢主要是因为av_find_stream_info 耗时久.下面给出重写查找音视频stream info的一段代码,用来替代av_find_stream_i ...
- python SendMail 发送邮件
最近在学习python 时,用到了发送邮件的操作,通过整理总结如下: 1.普通文本邮件 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和m ...
- matlab查找回车字符
Hi all, I would like to read the data all at once with: `file_text = fread(fid, inf, 'uint8=>char ...
- ios 存储学习笔记
一.主要路径: Library/Caches/此文件用于存储那些需要及可延迟或重创建的临时数据.且这些内容不会被IOS 系统备份,特别地,当设备磁盘空间不足且应用不在运行状态时,IOS 系统可能会移除 ...