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): 参数 描述 ...
随机推荐
- phpcms v9
栏目列表 {pc:content action="category" catid="$catid" num="34" siteid=&quo ...
- LintCode-Implement Iterator of Binary Search Tree
Design an iterator over a binary search tree with the following properties: Elements are visited in ...
- SQL Server备份事务日志结尾(Tail)
原文:http://blog.csdn.net/tjvictor/article/details/5256906 事务日志结尾经常提交数据库未备份的事务日志内容.基本上,每一次你执行事务日志备份时 ...
- eclipse 下找不到或无法加载主类的解决办法[转]
转自:http://blog.sina.com.cn/s/blog_7ebc46500101gtff.html 有时候 Eclipse 会发神经,好端端的 project 就这么编译不了了,连 Hel ...
- Spring中HibernateCallback的用法(转)
Hibernate的复杂用法HibernateCallback HibernateTemplate还提供一种更加灵活的方式来操作数据库,通过这种方式可以完全使用Hibernate的操作方式.Hiber ...
- uva 11627
二分 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #inc ...
- 微软发布WP SDK8.0 新增语音、应用内支付等原生API
http://www.csdn.net/article/2012-10-31/2811338-windows-phone-8-sdk 京时间10月30日,微软在旧金山举行新一代手机操作系统Window ...
- Boost简介
原文链接: 吴豆豆http://www.cnblogs.com/gdutbean/archive/2012/03/30/2425201.html Boost库 Boost库是为C++语言标准库提供扩 ...
- HDU1251 统计难题 Trie树
题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...
- Java Socket 使用BufferedWriter和BufferedReader要注意readLine 以及换行标志的发送
当接收的类使用的是BufferedReader,发送的类是BufferedWriter的时候,要注意发送的一行要有换行标识符. 请看下面一个例子,服务器接收不到客户端的信息. 服务器: import ...