Skip to content

Commit 5ef02a2

Browse files
committed
add permutation
1 parent 3b0ff98 commit 5ef02a2

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@
1313
<maven.compiler.target>22</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.junit.jupiter</groupId>
19+
<artifactId>junit-jupiter</artifactId>
20+
<version>RELEASE</version>
21+
<scope>test</scope>
22+
</dependency>
23+
</dependencies>
1624
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.function.Consumer;
6+
7+
/**
8+
* permutation or arrangement problem: [1,2,3] => [1,2,3] [1,3,2] ,[2,1,3] ,[2,3,1],[3,1,2],[3,2,1]
9+
*/
10+
public class Arrangement {
11+
12+
private int count;
13+
private ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
14+
private HashMap<Integer, Integer> map = new HashMap<>();
15+
16+
public void arrange(int[] arr) {
17+
18+
if (arr.length > 2) {
19+
for (int ele : arr) {
20+
map.put(arr.length - 1, ele);
21+
arrange(restOfArr(ele, arr));
22+
}
23+
} else if (arr.length == 2) {
24+
// System.out.println(arr[0] + " , " + arr[1]);
25+
// System.out.println(arr[1] + " , " + arr[0]);
26+
map.put(1, arr[1]);
27+
map.put(0, arr[0]);
28+
ArrayList<Integer> list = new ArrayList<>();
29+
for (int i = map.size() - 1; i >= 0; i--) {
30+
list.add(map.get(i));
31+
}
32+
result.add(list);
33+
map.put(1, arr[0]);
34+
map.put(0, arr[1]);
35+
ArrayList<Integer> list2 = new ArrayList<>();
36+
for (int i = map.size() - 1; i >= 0; i--) {
37+
list2.add(map.get(i));
38+
}
39+
result.add(list2);
40+
count++;
41+
count++;
42+
}
43+
}
44+
45+
protected int[] restOfArr(int ele, int[] arr) {
46+
int[] rest = new int[arr.length - 1];
47+
int i = 0;
48+
for (int e : arr) {
49+
if (ele == e) {
50+
continue;
51+
}
52+
rest[i] = e;
53+
i++;
54+
}
55+
return rest;
56+
}
57+
58+
public static void main(String[] args) {
59+
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
60+
Arrangement arrangement = new Arrangement();
61+
arrangement.arrange(arr);
62+
arrangement.result.stream().forEach(new Consumer<ArrayList<Integer>>() {
63+
@Override
64+
public void accept(ArrayList<Integer> integers) {
65+
for (int i : integers) {
66+
System.out.print(i + ", ");
67+
}
68+
System.out.println();
69+
}
70+
});
71+
System.out.println(arrangement.count);
72+
}
73+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.leetcode;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
7+
class ArrangementTest {
8+
9+
@Test
10+
public void restOfArrTest(){
11+
int[] arr = new int[]{1,2,3,4,5,6};
12+
int[] restOfArr = new Arrangement().restOfArr(1, arr);
13+
Assertions.assertArrayEquals(new int[]{2,3,4,5,6} , restOfArr);
14+
}
15+
16+
17+
}

0 commit comments

Comments
 (0)