这四个使用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排列组合问题的更多相关文章

  1. Codeforces 991E. Bus Number (DFS+排列组合)

    解题思路 将每个数字出现的次数存在一个数组num[]中(与顺序无关). 将出现过的数字i从1到num[i]遍历.(i from 0 to 9) 得到要使用的数字次数数组a[]. 对于每一种a使用排列组 ...

  2. dfs 排列组合——找所有子集(重复元素和不重复元素)

    17. 子集 中文 English 给定一个含不同整数的集合,返回其所有的子集. 样例 样例 1: 输入:[0] 输出: [ [], [0] ] 样例 2: 输入:[1,2,3] 输出: [ [3], ...

  3. DFS实现排列组合

    所谓排列,是指从给定的元素序列中依次取出元素,需要考虑取出顺序.比如,取出元素3, 5,因取出顺序的不同,则形成的序列{3, 5}与{5, 3}是不同的排列序列.对于长度为n的元素序列取出k个元素,则 ...

  4. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  5. Day4:T3搜索 T4数学题排列组合

    T3:搜索 很出名的题吧,费解的开关 同T2一样也是一题很考思考的 附上题解再解释吧: 对于每个状态,算法只需要枚举第一行改变哪些灯的状态,只要第一行的状态固定了,接下来的状态改变方法都是唯一的:每一 ...

  6. 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 ...

  7. LeetCode OJ:Combinations (排列组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  8. 学习sql中的排列组合,在园子里搜着看于是。。。

    学习sql中的排列组合,在园子里搜着看,看到篇文章,于是自己(新手)用了最最原始的sql去写出来: --需求----B, C, F, M and S住在一座房子的不同楼层.--B 不住顶层.C 不住底 ...

  9. .NET平台开源项目速览(11)KwCombinatorics排列组合使用案例(1)

    今年上半年,我在KwCombinatorics系列文章中,重点介绍了KwCombinatorics组件的使用情况,其实这个组件我5年前就开始用了,非常方便,麻雀虽小五脏俱全.所以一直非常喜欢,才写了几 ...

随机推荐

  1. SQL Server 08版与14版处理重复行的方式

    在项目中,利用循环拼接成了插入多行数据的SQL语句: Insert into table(col1,col2)vaules(value11,value21); Insert into table(co ...

  2. sqlserver锁表、解锁、查看销表

    锁定数据库的一个表 代码如下 复制代码 SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别 代码如下 复制代码 SELECT * FROM tab ...

  3. vue组件总结(三)

    一.什么是组件 组件(component)是Vue最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己的需要 ...

  4. Java后台调用gcc编译C语言代码

    想做一个能够在线编译代码运行的平台,Java和SQL已经支持了,因为是用Java写的后台,所以Java和SQL挺容易就实现了,做到支持C的时候就卡住了,网上搜了一下这种帖子好像很少. 我采取的办法是就 ...

  5. @TransactionConfiguration过时与替代写法

    在使用了Spring的项目中做单元测试时,以前的标准写法是这样的: 但是在高版本的Spring框架中(Spring4.2以后),@TransactionConfiguration已经标注为过时的注解, ...

  6. SQLServer事务的原理

    1.事务的概念 是数据库管理系统执行过程中的一个逻辑单元,由一个有限的数据库操作序列组成: 由事务开始(begin transaction)和事务结束(end transaction)之间执行的全体操 ...

  7. C#的接口基础教程之二 定义接口

    定义接口 从技术上讲,接口是一组包含了函数型方法的数据结构.通过这组数据结构,客户代码可以调用组件对象的功能. 定义接口的一般形式为: [attributes] [modifiers] interfa ...

  8. 5.7 并行复制配置 基于GTID 搭建中从 基于GTID的备份与恢复,同步中断处理

    5.7 并行复制配置 基于GTID 搭建中从 基于GTID的备份与恢复,同步中断处理 这个文章包含三个部分 1:gtid的多线程复制2:同步中断处理3:GTID的备份与恢复 下面文字相关的东西 大部分 ...

  9. Linux入门-第九周

    1.判断UID是否大于等于500,如果为真就显示为普通用户,如果为假就显示为系统或管理用户 AWK简介:awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报 ...

  10. 打开POST传参的弹出窗口

    //穿件 function openPostPopWindow(url,param,target){ var $form = $("<form></form>&quo ...