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年前就开始用了,非常方便,麻雀虽小五脏俱全.所以一直非常喜欢,才写了几 ...
随机推荐
- Liunx开发(Extjs4.1+desktop+SSH2超强视频教程实践)(1)
下周一出差宁波了,周六日就折腾点视频: 跟着视频教程开发,不过开发环境换linux,上月找工作,某个吉祥物是松鼠的公司要求用linux开发,没用过的,连面试机会都不给,极其高冷:好吧,咱就试试,用li ...
- Portal简介
Portal 在英语中是入口的意思.Portal 认证通常也称为 Web 认证,一般将 Portal 认 证网站称为门户网站. 未认证用户上网时,设备强制用户登录到特定站点,用户可以免费访问其中的服务 ...
- python实现剑指offer删除链表中重复的节点
题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...
- systemd 中的requires, wants, before, after
man systemd.unit man systemd.service ###依赖关系和前后顺序* 依赖关系:Requires和Wants * 前后顺序:After,Before 依赖关系,前 ...
- java基础编程——重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- 使用jquery实现获取除this(当前选定)以外的元素
今天做项目时,要求完成功能评价的分类,即好评,差评,中评.遇到一个问题,如何在选定一个评论类型时,该div颜色改变,其他评论类型的div颜色不变. 在使用$(this).attr()时,表示当前元素的 ...
- python-的多线程处理
书到用时方恨少,这句话在软件杯真的深深体会到了.但是对自己对于代码的领会能力还是有自信的,在做项目的时候用到了多线程的处理,开始都不知道该怎么去百度搜索关键词
- hibernate系列之二
首先先介绍一下持久化: 持久化:将程序数据在持久状态和瞬时状态间转换的机制:即将内存的数据永久存在关系型数据库中: 持久化类的编写规则: 持久化类需要提供无参构造方法: 持久化类的属性需要私有,对私有 ...
- Django之视图和URL配置
1.在创建项目时,Django会自动创建URL配置,在urls.py文件中 文件的默认内容如下所示: """mysite URL Configuration The ur ...
- Linux监控一之Nagios的安装与配置
一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报 ...