Skip to content

Commit 42e6378

Browse files
author
anduo
committed
排列组合问题
1 parent 28fc8d1 commit 42e6378

File tree

7 files changed

+261
-3
lines changed

7 files changed

+261
-3
lines changed

readme.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,25 @@ doc中有好几份文档,比较经典,可以参考。
255255
- [question03 balanced binary tree](./src/ninechapter/ch03_binary_tree_and_divide_conquer_and_dfs_bfs/Question03BalancedBinaryTree.java)
256256
- [question04 binary tree maximum path sum](./src/ninechapter/ch03_binary_tree_and_divide_conquer_and_dfs_bfs/Question04BinaryTreeMaximumPathSum.java)
257257

258-
259-
260-
258+
## chapter04 permutation & subset
259+
260+
排列组合问题总结
261+
- Arrays.sort 去重
262+
- result.add 什么时候输出结果
263+
- if(condition) continue 什么情况跳过
264+
- 回溯,关键是在递归完了,需要还原items的状态
265+
266+
排列组合适用题目
267+
- word break ii
268+
- palindrome partition
269+
- path sum ii
270+
- restore ip address
271+
- subsets ii
272+
- subsets
273+
- combinations
274+
- permutations ii
275+
- permutation
276+
- combination sum ii
277+
- substring with concatenation of all words
278+
- letter combination of a phone number
261279

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ninechapter.ch04_permutation_and_subset;
2+
// https://leetcode.com/problems/subsets/#/description
3+
// 子集
4+
// 给定一个含不同整数的集合,返回其所有的子集
5+
// http://www.lintcode.com/zh-cn/problem/subsets/
6+
// 如果 S = [1,2,3],有如下的解:
7+
//[
8+
// [3],
9+
// [1],
10+
// [2],
11+
// [1,2,3],
12+
// [1,3],
13+
// [2,3],
14+
// [1,2],
15+
// []
16+
//]
17+
18+
// 算法复杂度 2^n
19+
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
23+
/**
24+
* Created by anduo on 17-3-14.
25+
*/
26+
public class Question01SubSets {
27+
public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
28+
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
29+
if (nums == null || nums.length == 0) {
30+
return result;
31+
}
32+
Arrays.sort(nums);// 先排序是为了后边避免元素重复比如{1,2}和{2,1}
33+
34+
dfs(result, new ArrayList<>(), nums, 0);
35+
36+
return result;
37+
}
38+
39+
private void dfs(ArrayList<ArrayList<Integer>> result,
40+
ArrayList<Integer> items,
41+
int[] nums,
42+
int pos) {
43+
result.add(new ArrayList<>(items));
44+
45+
for (int i = pos; i < nums.length; i++) {
46+
items.add(nums[i]);
47+
dfs(result, items, nums, i + 1);
48+
// *回溯* 移除最后添加一个元素,相当于返回上一层的集合
49+
items.remove(items.size() - 1);
50+
}
51+
}
52+
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ninechapter.ch04_permutation_and_subset;
2+
// https://leetcode.com/problems/subsets/#/description
3+
// 子集
4+
// 给定一个含不同整数的集合,返回其所有的子集
5+
// http://www.lintcode.com/zh-cn/problem/subsets/
6+
// 如果 S = [1,2,3],有如下的解:
7+
//[
8+
// [3],
9+
// [1],
10+
// [2],
11+
// [1,2,3],
12+
// [1,3],
13+
// [2,3],
14+
// [1,2],
15+
// []
16+
//]
17+
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
21+
/**
22+
* Created by anduo on 17-3-14.
23+
*/
24+
public class Question02SubSetsII {
25+
public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
26+
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
27+
if (nums == null || nums.length == 0) {
28+
return result;
29+
}
30+
Arrays.sort(nums);// 先排序是为了后边避免元素重复比如{1,2}和{2,1}
31+
32+
dfs(result, new ArrayList<>(), nums, 0);
33+
34+
return result;
35+
}
36+
37+
private void dfs(ArrayList<ArrayList<Integer>> result,
38+
ArrayList<Integer> items,
39+
int[] nums,
40+
int pos) {
41+
result.add(new ArrayList<>(items));
42+
43+
for (int i = pos; i < nums.length; i++) {
44+
if (i > 0 && i != pos && nums[i] == nums[i - 1]) {
45+
continue;
46+
}
47+
items.add(nums[i]);
48+
dfs(result, items, nums, i + 1);
49+
// *回溯* 移除最后添加一个元素,相当于返回上一层的集合
50+
items.remove(items.size() - 1);
51+
}
52+
}
53+
54+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ninechapter.ch04_permutation_and_subset;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
// https://leetcode.com/problems/permutations/
7+
// 排列问题 时间复杂度 n!
8+
9+
10+
/**
11+
* Created by anduo on 17-3-14.
12+
*/
13+
public class Question03Permutations {
14+
public List<List<Integer>> permute(int[] nums) {
15+
List<List<Integer>> result = new ArrayList<>();
16+
dfs(result, new ArrayList<>(), nums);
17+
return result;
18+
}
19+
20+
private void dfs(List<List<Integer>> result,
21+
ArrayList<Integer> items,
22+
int[] nums) {
23+
if (items.size() == nums.length) {
24+
result.add(new ArrayList<>(items));
25+
}
26+
27+
for (int i = 0; i < nums.length; i++) {
28+
if (items.contains(nums[i])) {// 判断这个数是不是已经在排列中了
29+
continue;
30+
}
31+
items.add(nums[i]);
32+
dfs(result, items, nums);
33+
items.remove(items.size() - 1);
34+
}
35+
}
36+
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ninechapter.ch04_permutation_and_subset;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
// https://leetcode.com/problems/permutations-ii/
7+
// 排列问题 时间复杂度 n!
8+
9+
10+
/**
11+
* Created by anduo on 17-3-14.
12+
*/
13+
public class Question03PermutationsII {
14+
public List<List<Integer>> permute(int[] nums) {
15+
List<List<Integer>> result = new ArrayList<>();
16+
boolean[] visited = new boolean[nums.length];
17+
18+
dfs(result, new ArrayList<>(), nums, visited);
19+
return result;
20+
}
21+
22+
private void dfs(List<List<Integer>> result,
23+
ArrayList<Integer> items,
24+
int[] nums, boolean[] visited) {
25+
if (items.size() == nums.length) {
26+
result.add(new ArrayList<>(items));
27+
}
28+
29+
for (int i = 0; i < nums.length; i++) {
30+
if (visited[i] ||
31+
i != 0 && nums[i] == nums[i - 1] && !visited[i - 1]) {
32+
continue;
33+
}
34+
visited[i] = true;
35+
items.add(nums[i]);
36+
dfs(result, items, nums, visited);
37+
// 回溯->递归之后还原
38+
items.remove(items.size() - 1);
39+
visited[i] = false;
40+
}
41+
}
42+
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ninechapter.ch04_permutation_and_subset;
2+
3+
// https://leetcode.com/problems/permutation-sequence
4+
// 第k个排列
5+
// 给定 n 和 k,求123..n组成的排列中的第 k 个排列。
6+
// 对于 n = 3, 所有的排列如下:
7+
// 123
8+
// 132
9+
// 213
10+
// 231
11+
// 312
12+
// 321
13+
// 如果 k = 4, 第4个排列为,231.
14+
15+
/**
16+
* Created by anduo on 17-3-14.
17+
*/
18+
public class Question04PermutationSequence {
19+
20+
public String getPermutation(int n, int k) {
21+
22+
return null;
23+
}
24+
25+
26+
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ninechapter.ch04_permutation_and_subset;
2+
3+
import java.util.ArrayList;
4+
5+
// n皇后问题
6+
// n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击。
7+
// 给定一个整数n,返回所有不同的n皇后问题的解决方案。
8+
// 每个解决方案包含一个明确的n皇后放置布局,其中“Q”和“.”分别表示一个女王和一个空位置。
9+
10+
/**
11+
* Created by anduo on 17-3-14.
12+
*/
13+
public class Question05NQueens {
14+
/**
15+
* Get all distinct N-Queen solutions
16+
* @param n: The number of queens
17+
* @return: All distinct solutions
18+
* For example, A string '...Q' shows a queen on forth position
19+
*/
20+
ArrayList<ArrayList<String>> solveNQueens(int n) {
21+
// write your code here
22+
ArrayList<ArrayList<String>> result = new ArrayList<>();
23+
24+
return result;
25+
}
26+
}

0 commit comments

Comments
 (0)