-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
76 lines (60 loc) · 1.22 KB
/
Copy pathSolution.java
File metadata and controls
76 lines (60 loc) · 1.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package nextNumber;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Solution {
public static int next(int n) {
if (n < 10)
return n + 1;
else {
int res = n;
while (res < Math.pow(2, 31) && ! diff(digits(res), digits(n))) {
res++;
}
if (res > Math.pow(2, 31))
return -1;
else
return res;
}
}
public static void main(String[] args) {
System.out.println(next(654321));
}
public static List<Integer> digitsNumber(int n) {
List<Integer> digits = new ArrayList<>();
int res = n % 10;
digits.add(res);
n = n / 10;
while (n >= 10) {
res = n % 10;
digits.add(res);
n = n / 10;
}
digits.add(n);
return digits;
}
public static Set<Integer> digits(int n) {
Set<Integer> digits = new HashSet<Integer>();
int res = n % 10;
digits.add(res);
n = n / 10;
while (n >= 10) {
res = n % 10;
digits.add(res);
n = n / 10;
}
digits.add(n);
return digits;
}
public static boolean diff(Set<Integer> set1, Set<Integer> set2) {
if (set1 == null || set2 == null) {
return false;
}
for(Integer elt:set2) {
if(set1.contains(elt))
return false;
}
return true;
}
}