-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest33.java
More file actions
54 lines (49 loc) · 1.53 KB
/
test33.java
File metadata and controls
54 lines (49 loc) · 1.53 KB
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
package byteDance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by lizeyang on 2020/5/15.
* 输出给定数字下一个比它大的数字,比如输入:1234, 输出 1243
*/
public class test33 {
//利用全排列,bfs实现
public static int test(int num) {
char[] chars = String.valueOf(num).toCharArray();
List<String> list = new ArrayList<>();
partition(chars, 0, list);
Collections.sort(list);
if (list.get(list.size() - 1).equals(String.valueOf(num))) {
return -1;
} else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(String.valueOf(num))) {
return Integer.valueOf(list.get(i + 1));
}
}
}
return -1;
}
public static void partition(char[] chars, int index, List<String> list) {
if (index == chars.length - 1) {
String str = String.valueOf(chars);
if (!list.contains(str)) {
list.add(str);
}
}
for (int j = index; j < chars.length; j++) {
swap(chars, index, j);
partition(chars, index + 1, list);
swap(chars, j, index);
}
}
public static void swap(char[] chars, int a, int b) {
char tmp = chars[a];
chars[a] = chars[b];
chars[b] = tmp;
}
public static void main(String[] args) {
int num = 1234;
System.out.println(test(num));
}
}