还是有一定难度的。

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

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

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. Cocos2D-x搭建新环境注意事项

    网上资源都说安装Python后, 设置环境变量, 解压Cocos2Dx压缩包就OK, 但运行CppTest还是会报错, 以下是错误解决方案: 1. 错误提示 error LNK1123: failur ...

  2. 贱贱的美团安卓客户端---如何实现让安卓app在应用列表获得较靠前的位置

    起因: 自打愚安我开始使用android设备以来,一直觉得google还算厚道,应用列表里的顺序一直都是依据APP的名称,按照先中文(拼音字母表顺序),后英文(字母表顺序)的原则进行排序的,并没有说G ...

  3. 学习KnockOut第一篇之Hello World

    学习KnockOut第一篇之Hello World 笔者刚开始学习KnockOut.写的内容就相当于一个学习笔记.且在此处向官网致敬,比较喜欢他们家的Live Example版块,里面有jsFiddl ...

  4. android开发,assets下面的资源文件不会变化/改动

    我再调试asserts下面的资源文件,发现我改动assets下面的文件内容,在真机上测试的时候还是最原先的内容,没有变,后来,卸载,重装就ok了. 原因: assets下面的资源文件,若覆盖重装,则里 ...

  5. web.xml中servlet的配置

    <servlet>元素是配置Servlet所用的元素. <servlet-mapping>元素在Servlet和URL样式之间定义一个映射,即servlet类提供一个url,在 ...

  6. [nowCoder] 局部最小值位置

    定义局部最小的概念.arr长度为1时,arr[0]是局部最小.arr的长度为N(N>1)时,如果arr[0]<arr[1],那么arr[0]是局部最小:如果arr[N-1]<arr[ ...

  7. HTTP请求报文与响应报文

    http://docs.telerik.com/fiddler/KnowledgeBase/HTTP HTTP请求报文与响应报文 HTTP http://www.w3.org/Protocols/rf ...

  8. java 中 String 类的几个问题

    首先,我们要搞清楚,在java中,引用和基本数据类型是存储在栈中的.而对象是存储在堆中的. 只有一个例外,就是String对象. 例如: String str1="test"; S ...

  9. APT攻击

    http://netsecurity.51cto.com/art/201211/363040.htm

  10. POJ2676Sudoku

    http://poj.org/problem?id=2676 题意 : 这个是我最喜欢玩的数独了,就是一个9乘9的宫格,填上1到9九个数字,每行每列每个宫格之内不能有重复的数字,给出的九宫格中,0是待 ...