还是有一定难度的。

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

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

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; }
};

最新文章

  1. 转贴:让Windows 2008 R2 64bit支持ASP.NET 1.1应用程序
  2. ELK日志管理之——kibana部署
  3. [New Portal]Windows Azure Cloud Service (34) TechEd 2013 North America关于Azure的最新消息
  4. Python之路【第六篇】:面向对象编程相关
  5. php_curl模拟登录有验证码实例
  6. u-boot中分区和内核MTD分区关系
  7. Unknown
  8. mysql 和 mongo db 语法对比
  9. eclipse背景颜色修改插件color theme
  10. CentOS 7 服务器配置--安装MongoDB
  11. iscroll使用之页面卡顿问题
  12. Codeforces 839E Mother of Dragons【__builtin_popcount()的使用】
  13. Eclipse+Resin开发环境迁移中发生的一些问题
  14. Angular使用总结 ---以密码确认为例实现模版驱动表单的自定义校验
  15. java 11 Stream 加强
  16. cycle标签和random两种方式美化表格
  17. 进入页面就触发了popstate事件。
  18. Maven配置 和创建一个Maven项目
  19. 关于yum网络版仓库(本地yum仓库的安装配置,如果没网了,做一个局域网内的yum仓库)
  20. STC15单片机最小系统DIY

热门文章

  1. eclipse不能创建java虚拟机-解决方法
  2. mysql 的存储过程调试软件
  3. Understand User&#39;s Intent from Speech and Text
  4. Eclipse maven工程 Missing artifact com.sun:tools:jar:1.5.0:system 解决方法
  5. HDOJ 3547 DIY Cube 解题报告
  6. 【BZOJ】【2819】NIM
  7. windows 64位整数
  8. linux网卡速率和双工模式的配置
  9. hdoj 1596 find the safest road
  10. zju 1037 Gridland(找规律,水题)