11/5 <backtracking> 伪BFS+回溯
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+回溯的更多相关文章
- Pots POJ - 3414【状态转移bfs+回溯】
典型的倒水问题: 即把两个水杯的每种状态视为bfs图中的点,如果两种状态可以转化,即可认为二者之间可以连一条边. 有3种倒水的方法,对应2个杯子,共有6种可能的状态转移方式.即相当于图中想走的方法有6 ...
- POJ 3414--Pots(BFS+回溯路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9963 Accepted: 4179 Special Ju ...
- hdu--1072--Nightmare(bfs回溯)
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- POJ——3984迷宫问题(BFS+回溯)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14568 Accepted: 8711 Description ...
- 1131 Subway Map DFS解法 BFS回溯!
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 广度优先算法BFS
package myalgorithm; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; / ...
- 最少步数(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 ...
随机推荐
- javascript 写一个 map方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 截图自动添加水印图片工具 pickpick设置中文语言
推荐一款截图工具,主要是可以截图自动带水印,效果不错 最近发现我的不少文章被转载的到处都是.乱七八糟,这个功能后续准备做个水印用起来,感觉不错 主角介绍 首先介绍下主角 PickPick
- 搜索旋转排序数组II
题目 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [,,,,,,] 可能变为 [,,,,,,] ). 编写一个函数来判断给定的目标值是否存在于数组中.若存在返回 true, ...
- Spring源码系列 — BeanDefinition扩展点
前言 前文介绍了Spring Bean的生命周期,也算是XML IOC系列的完结.但是Spring的博大精深,还有很多盲点需要摸索.整合前面的系列文章,从Resource到BeanDefinition ...
- WPF-自定义实现步骤条控件
步骤条实现的效果: 步骤条控件是在listbox的基础上实现的. 一. xaml代码: <Window.Resources> <convert1:StepListBarWidthCo ...
- jakarta-oro-2.0.8.jar-----------JAVA FTP相关
资源不好找,找到了就和大家分享一下! 链接:https://share.weiyun.com/51kBB0y 密码:2hcxcu
- 关于 L3 缓存行 cacheLIne 的研究!还是对程序有举足轻重的作用!
https://www.cnblogs.com/PurpleTide/archive/2010/11/25/1887506.html CLR via C# 读书笔记 2-3 Cache Lines a ...
- datatable转layui表格v2[分页and带模板]【偏实例】
本项目由普通mvc+webapi接口构成.按执行顺序,代码如下:主控制器:public ActionResult Index(int id=0) { ViewData["myid" ...
- laravel Method Illuminate\Validation\Validator::validateReuqired does not exist.
Method Illuminate\Validation\Validator::validateReuqired does not exist. 此错误是由于我们在配置验证时,写错了关键字, publ ...
- FCC-学习笔记 Missing letters
FCC-学习笔记 Missing letters 1>最近在学习和练习FCC的题目.这个真的比较的好,推荐给大家. 2>中文版的地址:https://www.freecodecamp.c ...