Skip to content

Commit 99acaf0

Browse files
author
applewjg
committed
permutations
Change-Id: I4b4cac41ef886ca1bc53090fa500f433873d5262
1 parent 2e1678b commit 99acaf0

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

Permutations.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Permutations
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/permutations/
7+
Notes:
8+
Given a collection of numbers, return all possible permutations.
9+
For example,
10+
[1,2,3] have the following permutations:
11+
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
12+
13+
Solution: dfs...
14+
*/
15+
public class Solution {
16+
public List<List<Integer>> permute_1(int[] num) {
17+
List<List<Integer>> res = new ArrayList<List<Integer>>();
18+
List<Integer> path = new ArrayList<Integer>();
19+
boolean[] free = new boolean[num.length];
20+
Arrays.fill(free, true);
21+
permuteRe(num, res, path,free);
22+
return res;
23+
}
24+
void permuteRe(int[] num, List<List<Integer>> res, List<Integer> path, boolean[] free) {
25+
if(path.size() == num.length) {
26+
ArrayList<Integer> tmp = new ArrayList<Integer>(path);
27+
res.add(tmp);
28+
return;
29+
}
30+
for (int i = 0; i < num.length; ++i) {
31+
if (free[i] == true) {
32+
free[i] = false;
33+
path.add(num[i]);
34+
permuteRe(num, res, path, free);
35+
path.remove(path.size() - 1);
36+
free[i] = true;
37+
}
38+
}
39+
}
40+
public boolean nextPermutation(int[] num) {
41+
int last = num.length - 1;
42+
int i = last;
43+
while (i > 0 && num[i - 1] >= num [i]) --i;
44+
for (int l = i, r = last; l < r; ++l, --r) {
45+
num[l] = num[l] ^ num[r];
46+
num[r] = num[l] ^ num[r];
47+
num[l] = num[l] ^ num[r];
48+
}
49+
if (i == 0) {
50+
return false;
51+
}
52+
int j = i;
53+
while (j <= last && num[i-1] >= num[j]) ++j;
54+
num[i-1] = num[i-1] ^ num[j];
55+
num[j] = num[i-1] ^ num[j];
56+
num[i-1] = num[i-1] ^ num[j];
57+
return true;
58+
}
59+
public List<List<Integer>> permute_2(int[] num) {
60+
List<List<Integer>> res = new ArrayList<List<Integer>>();
61+
Arrays.sort(num);
62+
do {
63+
List<Integer> path = new ArrayList<Integer>();
64+
for (int i : num) path.add(i);
65+
res.add(path);
66+
} while(nextPermutation(num));
67+
return res;
68+
}
69+
}

PermutationsII.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Permutations II
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/permutations-ii/
7+
Notes:
8+
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
9+
For example,
10+
[1,1,2] have the following unique permutations:
11+
[1,1,2], [1,2,1], and [2,1,1].
12+
13+
Solution: dfs...
14+
*/
15+
public class Solution {
16+
public List<List<Integer>> permuteUnique_1(int[] num) {
17+
List<List<Integer>> res = new ArrayList<List<Integer>>();
18+
List<Integer> path = new ArrayList<Integer>();
19+
Arrays.sort(num);
20+
boolean[] visited = new boolean[num.length];
21+
Arrays.fill(visited, false);
22+
permuteRe(num, res, path,visited);
23+
return res;
24+
}
25+
void permuteRe(int[] num, List<List<Integer>> res, List<Integer> path, boolean[] visited) {
26+
if(path.size() == num.length) {
27+
ArrayList<Integer> tmp = new ArrayList<Integer>(path);
28+
res.add(tmp);
29+
return;
30+
}
31+
for (int i = 0; i < num.length; ++i) {
32+
if(visited[i]||(i!=0&&num[i-1]==num[i]&&visited[i-1])) continue;
33+
visited[i] = true;
34+
path.add(num[i]);
35+
permuteRe(num, res, path, visited);
36+
path.remove(path.size() - 1);
37+
visited[i] = false;
38+
}
39+
}
40+
public boolean nextPermutation(int[] num) {
41+
int last = num.length - 1;
42+
int i = last;
43+
while (i > 0 && num[i - 1] >= num [i]) --i;
44+
for (int l = i, r = last; l < r; ++l, --r) {
45+
num[l] = num[l] ^ num[r];
46+
num[r] = num[l] ^ num[r];
47+
num[l] = num[l] ^ num[r];
48+
}
49+
if (i == 0) {
50+
return false;
51+
}
52+
int j = i;
53+
while (j <= last && num[i-1] >= num[j]) ++j;
54+
num[i-1] = num[i-1] ^ num[j];
55+
num[j] = num[i-1] ^ num[j];
56+
num[i-1] = num[i-1] ^ num[j];
57+
return true;
58+
}
59+
public List<List<Integer>> permuteUnique_2(int[] num) {
60+
List<List<Integer>> res = new ArrayList<List<Integer>>();
61+
Arrays.sort(num);
62+
do {
63+
List<Integer> path = new ArrayList<Integer>();
64+
for (int i : num) path.add(i);
65+
res.add(path);
66+
} while(nextPermutation(num));
67+
return res;
68+
}
69+
}

0 commit comments

Comments
 (0)