还是有一定难度的。

基本方法,就是用队列,然后不断累加新的数。这是为了不重复而量身定制的。

如果运行重复,是有更简单清晰的方法,就是每次增加考虑一个数字,然后加到本来每一个结果的后面。如下:

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(需要思考,包括了子数组的求法)的更多相关文章

  1. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  2. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  3. 求数组的子数组之和的最大值II

    这次在求数组的子数组之和的最大值的条件下又增加了新的约束:  1.要求数组从文件读取.      2.如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保 ...

  4. lintcode循环数组之连续子数组求和

    v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...

  5. C#中求数组的子数组之和的最大值

    <编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...

  6. lintcode:子数组之和为0

    题目: 子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3]. 解 ...

  7. 最大连续子数组问题2-homework-02

    1) 一维数组最大连续子数组 如第homework-01就是一维数组的最大子数组,而当其首位相接时,只需多考虑子数组穿过相接的那个数就行了! 2)二维数组 算法应该和第一次的相似,或者说是将二维转化为 ...

  8. 【剑指offer】连续子数组的最大和

    个開始,到第3个为止).你会不会被他忽悠住? 输入: 输入有多组数据,每组測试数据包括两行. 第一行为一个整数n(0<=n<=100000),当n=0时,输入结束.接下去的一行包括n个整数 ...

  9. 将数组分割为几个等长度的子数组(使用slice)

    先了解一下slice方法: slice() 1.定义:slice()可从已有数组中截取返回指定的元素,形成一个新的数组: 语法:arrayObject.slice(start,end): 参数 描述 ...

随机推荐

  1. 什么是 block

    什么是 block 1.提前准备好的一段可以执行的代码 2.block 可以当做参数传递 3.在需要的时候执行 block 4,block 中使用 self 时肯产生循环引用 block 做网络异步耗 ...

  2. LoadRunner 学习笔记(1)性能测试常见术语

    并发用户数据:与服务器进行交互的在线用户数量 请求响应时间:从Client端发出请求到得到响应的整个时间 一般包括网络响应时间 + server的响应时间 事务请求响应时间:完成这个事务所用的时间 这 ...

  3. bnuoj 16493 Just Pour the Water(矩阵快速幂)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...

  4. 高德开发 android 出现 key 鉴权失败

    环境windows + android studio 原因: 曾经更改过key.store 解决办法: 首先运行cmd移动到keystore的目录下keytool -list -keystore 文件 ...

  5. 不用安装语言包,教你将PS界面语言修改成默认语言(英语)

    地址:http://www.cnblogs.com/Loonger/p/5112020.html 嗯,干脆利落,直接上教程.超简单的方法.(该方法可以随时在你已有语言[非英语]和PS默认语言[英语]之 ...

  6. 剑指offer--面试题15

    题目:打印单向链表中倒数第k个节点 以下为自己所写代码,未经过验证,只是写个思路... #include<iostream> #include<vector> #include ...

  7. 剑指offer--面试题4

    题目:替换字符串中的空格为“%20”. 说明:在浏览器的地址栏中输入某个网址,在解析过程中会看到类似“%20”的字样,这应该就是网络编程涉及的内容... 该题总体来说比较简单(连我都能想到!),个人认 ...

  8. CentOS安装RockMongo

    rockmongo官网下载页面在这里: http://rockmongo.com/downloads 找到最新版本的下载链接,一般第一个就是: 右键复制url,比如说是这个: http://rockm ...

  9. MATLAB——axis

    MATLAB——axis axis中文为“轴”之意,在matlab中用于控制坐标轴的范围和样式(颜色等). axis([XMIN XMAX YMIN YMAX]) 设置当前所绘图像的x轴和y轴的范围. ...

  10. Codeforces Round #256 (Div. 2) Multiplication Table

    C题, #include<cstdio> #include<cstring> #include<algorithm> #define maxn 5005 using ...