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. JAVA基础系列:Object类

    1. 万物皆对象 1. JVM在编译源代码时,在遇到没有继承Object的对象的时候,编译器会默认指定一个默认的父类Object 2. Object 和接口的关系,接口是否继承Object?接口没有继 ...

  2. windows上安装python2和python3虚拟环境

    一.windows上安装 1.安装python 分别安装了Python2和Python3 python3.7默认安装目录 C:\Users\Administrator\AppData\Local\Pr ...

  3. 解决office365无法登录以及同步的问题

    解决office365无法登录以及同步的问题 You better need to test them one by one. You better need to test them one by ...

  4. 在项目中使用Solr

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  5. css彩虹文字

    用CSS3实现彩虹文字的效果,只在Webkit内核的浏览器(谷歌浏览器或移动端)上有效果. background-image: -webkit-gradient(linear, left top, r ...

  6. CentOS系统安装Python3

    准备: CentOS 6.4系统 Python-3.6.5.tgz 下载地址: 官网:https://www.python.org/downloads/release/python-365/ 镜像:h ...

  7. Logstash:多个配置文件(conf)

    Logstash:多个配置文件(conf) 对于多个配置的处理方法,有多个处理方法: 多个pipeline 一个pipleline处理多个配置文件 一个pipeline含有一个逻辑的数据流,它从inp ...

  8. 转 让FPGA替代GPU的6大顾虑,你确定不看看吗?

    最近FPGA又频频被各AI领域的巨头看好,比如微软.百度.科大讯飞都对FPGA应用前景有所期待.那么如果让你选择FPGA作为AI计算系统的主力军,你会有什么样的顾虑? 这几天,已经退役的AlphaGo ...

  9. OpenGL入门1.4:纹理/贴图Texture

    每一个小步骤的源码都放在了Github 的内容为插入注释,可以先跳过 前言 游戏玩家对Texture这个词应该不陌生,我们已经知道了怎么为每个顶点添加颜色来增加图形的细节,但,如果想让图形看起来更真实 ...

  10. 绑定 Binding Path=.,Binding.,Binding Source={StaticResource ResourceKey="Hello"} xmlns:sys="clr-namespace:System;assembly=mscorlib"

    xmlns:sys="clr-namespace:System;assembly=mscorlib" <Window.Resources> <Style Targ ...