DFS排列组合问题
这四个使用DFS来求解所有组合和排列的例子很有代表性,这里做一个总结:
1.不带重复元素的子集问题
public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
// write your code here
ArrayList<ArrayList<Integer>> results = new ArrayList<>();
if (nums == null || nums.length == 0) {
return results;
}
Arrays.sort(nums);
DFS(results, new ArrayList<Integer>(), nums, 0);
return results;
}
public void DFS(ArrayList<ArrayList<Integer>> results, ArrayList<Integer> cur,
int[] nums, int start) {
results.add(new ArrayList<Integer>(cur));
for (int i = start; i < nums.length; i++) {
cur.add(nums[i]);
DFS(results, cur, nums, i+1);
cur.remove(cur.size()-1);
}
}
2.带重复元素的子集问题
public ArrayList<ArrayList<Integer>> subsetsWithDup(ArrayList<Integer> S) {
// write your code here
ArrayList<ArrayList<Integer>> results = new ArrayList<>();
if (S == null || S.size() == 0) {
return results;
}
Collections.sort(S);
DFS(results, new ArrayList<Integer>(), S, 0);
return results;
}
public void DFS(ArrayList<ArrayList<Integer>> results,
ArrayList<Integer> cur,
ArrayList<Integer> S,
int start) {
results.add(new ArrayList<>(cur));
for (int i = start; i < S.size(); i++) {
if(i != start && S.get(i) == S.get(i - 1)) {
continue;
}
cur.add(S.get(i));
DFS(results, cur, S, i+1);
cur.remove(cur.size()-1);
}
}
3.不带重复元素的全排列问题
public List<List<Integer>> permute(int[] nums) {
// write your code here
List<List<Integer>> results = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0) {
results.add(new ArrayList<Integer>());
return results;
}
boolean[] used = new boolean[nums.length];
DFS(results, new ArrayList<Integer>(), nums, used);
return results;
}
public void DFS(List<List<Integer>> results, List<Integer> cur, int[] nums, boolean[] used) {
if (cur.size() == nums.length) {
results.add(new ArrayList<Integer>(cur));
return;
}
for(int i = 0; i<nums.length; i++) {
if (used[i]) {
continue;
}
used[i] =true;
cur.add(nums[i]);
DFS(results, cur, nums, used);
used[i] =false;
cur.remove(cur.size()-1);
}
}
4.带重负元素的全排列问题
public List<List<Integer>> permuteUnique(int[] nums) {
// Write your code here
List<List<Integer>> results = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0) {
results.add(new ArrayList<Integer>());
return results;
}
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
DFS(results, new ArrayList<Integer>(), used, nums);
return results;
}
public void DFS(List<List<Integer>> results, List<Integer> cur, boolean[] used, int[] nums) {
if (cur.size() == nums.length) {
results.add(new ArrayList<Integer>(cur));
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i]) {
continue;
}
if (i > 0 && nums[i] == nums[i - 1] && !used[i-1]) {
continue;
}
used[i] = true;
cur.add(nums[i]);
DFS(results, cur, used, nums);
used[i] = false;
cur.remove(cur.size() -1);
}
}
寻找丢失的数 II*
给一个由 1 - n 的整数随机组成的一个字符串序列,其中丢失了一个整数,请找到它。
回溯,当前位置可以单独,也可以和下一个结合,当前为0一定不行。curIndex控制啥时候结束。
public int findMissing2(int n, String str) {
// Write your code here
if (n < 1 || str == null) {
return 0;
}
char[] chars = str.toCharArray();
boolean[] appeared = new boolean[n + 1];
int[] curIndex = {0};
help(appeared, chars, curIndex, n);
for (int i = 1; i < appeared.length; i++) {
if (!appeared[i]) {
return i;
}
}
return -1;
}
public void help(boolean[] appeared, char[] chars, int[] curIndex, int n) {
if (curIndex[0] >= chars.length) {
return;
}
if (chars[curIndex[0]] == '0') {
return;
}
if (!appeared[chars[curIndex[0]] - '0']) {
appeared[chars[curIndex[0]] - '0'] = true;
curIndex[0]++;
help(appeared, chars, curIndex, n);
if (curIndex[0] >= chars.length) {
return;
}
curIndex[0]--;
appeared[chars[curIndex[0]] - '0'] = false;
}
if (curIndex[0] < chars.length - 1) {
int c1 = chars[curIndex[0]] - '0';
int c2 = chars[curIndex[0] + 1] - '0';
int newnum = c1 * 10 + c2;
if (newnum <= n && !appeared[newnum]) {
appeared[newnum] = true;
curIndex[0] += 2;
help(appeared, chars, curIndex, n);
if (curIndex[0] >= chars.length) {
return;
}
curIndex[0]-=2;
appeared[newnum] = false;
}
}
}
DFS排列组合问题的更多相关文章
- Codeforces 991E. Bus Number (DFS+排列组合)
解题思路 将每个数字出现的次数存在一个数组num[]中(与顺序无关). 将出现过的数字i从1到num[i]遍历.(i from 0 to 9) 得到要使用的数字次数数组a[]. 对于每一种a使用排列组 ...
- dfs 排列组合——找所有子集(重复元素和不重复元素)
17. 子集 中文 English 给定一个含不同整数的集合,返回其所有的子集. 样例 样例 1: 输入:[0] 输出: [ [], [0] ] 样例 2: 输入:[1,2,3] 输出: [ [3], ...
- DFS实现排列组合
所谓排列,是指从给定的元素序列中依次取出元素,需要考虑取出顺序.比如,取出元素3, 5,因取出顺序的不同,则形成的序列{3, 5}与{5, 3}是不同的排列序列.对于长度为n的元素序列取出k个元素,则 ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- Day4:T3搜索 T4数学题排列组合
T3:搜索 很出名的题吧,费解的开关 同T2一样也是一题很考思考的 附上题解再解释吧: 对于每个状态,算法只需要枚举第一行改变哪些灯的状态,只要第一行的状态固定了,接下来的状态改变方法都是唯一的:每一 ...
- 2017ACM暑期多校联合训练 - Team 1 1006 HDU 6038 Function (排列组合)
题目链接 Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m ...
- LeetCode OJ:Combinations (排列组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 学习sql中的排列组合,在园子里搜着看于是。。。
学习sql中的排列组合,在园子里搜着看,看到篇文章,于是自己(新手)用了最最原始的sql去写出来: --需求----B, C, F, M and S住在一座房子的不同楼层.--B 不住顶层.C 不住底 ...
- .NET平台开源项目速览(11)KwCombinatorics排列组合使用案例(1)
今年上半年,我在KwCombinatorics系列文章中,重点介绍了KwCombinatorics组件的使用情况,其实这个组件我5年前就开始用了,非常方便,麻雀虽小五脏俱全.所以一直非常喜欢,才写了几 ...
随机推荐
- LeetCode Best Time to Buy and Sell Stock II (简单题)
题意: 股票买卖第2题.给出每天的股票价格,每次最多买一股,可以多次操作,但是每次在买之前必须保证身上无股票.问最大的利润? 思路: 每天的股票价格可以看成是一条曲线,能卖掉就卖掉,那么肯定是在上升的 ...
- [Asp.Net] Form验证中 user.identity为false
这个方法可以是user.identity设置为true FormsAuthentication.SetAuthCookie(Username, true); 但是要开启form验证, 在配置文件中 & ...
- 使用AirDroid控制百度影棒
十一假期的时候看到有促销活动买了一个百度影棒2S+.话说这东西当做普通家庭客厅的电视盒子还差点火候,不论是操作还是内容用起来都有点折腾.(当然,有些是形势所迫,大家都懂.) 不过,如果就把它看作是普通 ...
- IOS segue(跳转页面处理)
● Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue) Segue的属性 ● 每一个Segue对象,都有3个属性 ➢ 唯一标识 @prope ...
- POJ-1459 Power Network---最大流
题目链接: https://cn.vjudge.net/problem/POJ-1459 题目大意: 简单的说下题意(按输入输出来讲,前面的描述一堆的rubbish,还用来误导人),给你n个点,其中有 ...
- CSS3 - - Media(css3媒介查询) 属性
语法结构及用法: @media 设备名 only (选取条件) not (选取条件) and(设备选取条件),设备二{sRules} 实际应用一 判断设备横竖屏: /* 这是匹配横屏的状态,横屏时的 ...
- Java环境变量搭建(Windows环境)
变量名:JAVA_HOME 变量值:C:\Program Files (x86)\Java\jdk1.8.0_91 // 要根据自己的实际路径配置 变量名:CLASSPATH 变量值:. ...
- javaweb基础(29)_EL表达式
一.EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数 ...
- Java代码工具箱之解析单行单列简单Excel
1. 使用开源工具 jxl.jar 2. 功能:解析常规Excel.xls格式测试可行,xlsx未测试.Excel测试格式为常规类似table这种简单布局文件.第一行为标题,后面行为内容.代码 可正确 ...
- ajax全局变量的使用
var username; $.ajax({ type:"post", url:"a.action", data: {}, dataType: 'text', ...