-
Notifications
You must be signed in to change notification settings - Fork 58
/
Day18.java
61 lines (48 loc) · 1.08 KB
/
Day18.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.offer;
/**
* 数组的全排列
* @author kexun
*
*/
public class Day18 {
public static void main(String[] args) {
char[] array = {
'a','b','c'
};
Day18 d = new Day18();
d.allSort(array);
}
public void allSort(char[] array) {
if (array.length == 0)
return;
sort(array, 0);
}
/**
* 通过递归的方式, 将数组分成两部分, 第一个字符 和 后面一部分, 然后将第一个字符与后面全都互换一边
* 如此递归调用,通过 n!此交换 可以将数组全排列一边
* @param array
* @param begin
*/
public void sort(char[] array, int begin) {
int length = array.length;
if (begin == length) {
print(array);
} else {
for (int i = begin; i < length; i++) {
char temp = array[i];
array[i] = array[begin];
array[begin] = temp;
sort(array, begin+1);
temp = array[i];
array[i] = array[begin];
array[begin] = temp;
}
}
}
public void print(char[] array) {
for (char c : array) {
System.out.print(c+",");
}
System.out.println();
}
}