78. Subsets

While iterating through all numbers, for each new number, we can either pick it or not pick it
1, if pick, just add current number to every existing subset.
2, if not pick, just leave all existing subsets as they are.
We just combine both into our result.

For example, {1,2,3} intially we have an emtpy set as result [ [ ] ]
Considering 1, if not use it, still [ ], if use 1, add it to [ ], so we have [1] now
Combine them, now we have [ [ ], [1] ] as all possible subset

Next considering 2, if not use it, we still have [ [ ], [1] ], if use 2, just add 2 to each previous subset, we have [2], [1,2]
Combine them, now we have [ [ ], [1], [2], [1,2] ]

Next considering 3, if not use it, we still have [ [ ], [1], [2], [1,2] ], if use 3, just add 3 to each previous subset, we have [ [3], [1,3], [2,3], [1,2,3] ]
Combine them, now we have [ [ ], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3] ]

class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
for(int n : nums){
int size = result.size();
for(int i = 0; i < size; i++){
List<Integer> subset = new ArrayList<>(result.get(i));
subset.add(n);
result.add(subset);
}
}
return result;
}
}

90. Subsets II

我们用 lastAdded 来记录上一个处理的数字,然后判定当前的数字和上面的是否相同,若不同,则循环还是从0到当前子集的个数,若相同,则新子集个数减去之前循环时子集的个数当做起点来循环

class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<List<Integer>>();
result.add(new ArrayList<Integer>());
int lastAdded = 0;
for(int i = 0; i < nums.length; i++){
if(i == 0 || nums[i] != nums[i-1]) lastAdded = 0;
int size = result.size();
for(int j = lastAdded; j < size; j++){
List<Integer> cur = new ArrayList<Integer>(result.get(j));
cur.add(nums[i]);
result.add(cur);
}
lastAdded = size;
}
return result;
}
}

11/5 <backtracking> 伪BFS+回溯的更多相关文章

  1. Pots POJ - 3414【状态转移bfs+回溯】

    典型的倒水问题: 即把两个水杯的每种状态视为bfs图中的点,如果两种状态可以转化,即可认为二者之间可以连一条边. 有3种倒水的方法,对应2个杯子,共有6种可能的状态转移方式.即相当于图中想走的方法有6 ...

  2. POJ 3414--Pots(BFS+回溯路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9963   Accepted: 4179   Special Ju ...

  3. hdu--1072--Nightmare(bfs回溯)

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  4. POJ 3414 Pots(BFS+回溯)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11705   Accepted: 4956   Special J ...

  5. POJ——3984迷宫问题(BFS+回溯)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14568   Accepted: 8711 Description ...

  6. 1131 Subway Map DFS解法 BFS回溯!

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  7. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  8. 广度优先算法BFS

    package myalgorithm; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; / ...

  9. 最少步数(dfs + bfs +bfs优化)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

随机推荐

  1. word设置标题1,标题2,标题3项目编号联动

    word经常要按层级显示标题,并且标题样式之间要联动 如: 设置办法: word内鼠标右键 2.选择项目符号和编号 3.选择一个样式并自定义 将级别链接到样式.选择对应标题样式即可实现联动.

  2. Python 下载图片的三种方法

    import os os.makedirs('./image/', exist_ok=True) IMAGE_URL = "http://image.nationalgeographic.c ...

  3. 压缩20M文件从30秒到1秒的优化过程

    文章来源公众号:IT牧场 有一个需求需要将前端传过来的10张照片,然后后端进行处理以后压缩成一个压缩包通过网络流传输出去.之前没有接触过用Java压缩文件的,所以就直接上网找了一个例子改了一下用了,改 ...

  4. 解决Git 克隆代码 The remote end hung up unexpectedly错误

    从GitHub上克隆一个项目一直不成功!猜想可能是文件太大超时或者网络太慢超时! 解决方案: 配置 git config -- git config -- 增加最低速时间,but,还是不行! 公司网络 ...

  5. .net基础加强

    1.冒泡排序 请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序 , , , , , , , , , }; BubbleSort(num); C ...

  6. 关于创建node服务

    1.环境条件准备: A.确定node已经创建 B.npm或cnpm已经下载,npm和cnpm其实是一个道理 C.mysql或者使用其他数据库已经安装(本例使用mysql) 2.开始创建,首先新建一个文 ...

  7. js中对象字面量

    一.对象字面量语法 var person={ name:'小王', age:18, _pri:233 } 成员名称的单引号不是必须的 最后一个成员结尾不要用逗号,不然在某些浏览器中会抛出错误 成员名相 ...

  8. Android培训准备资料之五大布局简单介绍

    本篇博客主要简单的给大家介绍一下Android五大布局 (1)LinearLayout(线性布局) (2)RelativeLayout(相对布局) (3)FrameLayout(帧布局) (4)Abs ...

  9. SparkStreming中 `transform()`算子的 的使用

    关联 DStream 和 RDD transform(func) Return a new DStream by applying a RDD-to-RDD function to every RDD ...

  10. Centos7 安装使用virtualenvwrapper

    退出所有的虚拟环境,在物理环境下安装 1.下载安装virtualenvwrapper pip3 install virtualenvwrapper 2.查看python3的文件和virtualenvw ...