forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_2566.java
54 lines (51 loc) · 1.82 KB
/
_2566.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _2566 {
public static class Solution1 {
public int minMaxDifference(int num) {
List<Integer> digits = new ArrayList<>();
while (num != 0) {
digits.add(num % 10);
num /= 10;
}
int toReplace = Integer.MAX_VALUE;
List<Integer> maxDigits = new ArrayList<>();
for (int i = digits.size() - 1; i >= 0; i--) {
if (toReplace == Integer.MAX_VALUE && digits.get(i) != 9) {
toReplace = digits.get(i);
maxDigits.add(9);
} else if (digits.get(i) == toReplace) {
maxDigits.add(9);
} else {
maxDigits.add(digits.get(i));
}
}
int max = 0;
int times = 1;
for (int i = maxDigits.size() - 1; i >= 0; i--) {
max += maxDigits.get(i) * times;
times *= 10;
}
toReplace = Integer.MIN_VALUE;
List<Integer> minDigits = new ArrayList<>();
for (int i = digits.size() - 1; i >= 0; i--) {
if (toReplace == Integer.MIN_VALUE && digits.get(i) != 0) {
toReplace = digits.get(i);
minDigits.add(0);
} else if (digits.get(i) == toReplace) {
minDigits.add(0);
} else {
minDigits.add(digits.get(i));
}
}
int min = 0;
times = 1;
for (int i = minDigits.size() - 1; i >= 0; i--) {
min += minDigits.get(i) * times;
times *= 10;
}
return max - min;
}
}
}