Skip to content

Commit 0d542d3

Browse files
add 1432
1 parent fde94d6 commit 0d542d3

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1432|[Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1432.java) | |Medium|String|
1112
|1431|[Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1431.java) | |Easy|Array|
1213
|1423|[Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1423.java) | |Medium|Array, DP, Sliding Window|
1314
|1422|[Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1422.java) | |Easy|String|
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1432 {
4+
public static class Solution1 {
5+
public int maxDiff(int num) {
6+
int smallest = getSmallest(num);
7+
int biggest = getBiggest(num);
8+
return biggest - smallest;
9+
}
10+
11+
private int getBiggest(int num) {
12+
int firstDigit = Integer.parseInt(Integer.toString(num).substring(0, 1));
13+
if (firstDigit == 9) {
14+
String remainder = Integer.toString(num).substring(1);
15+
int numberToChangeToNine = 0;
16+
for (char c : remainder.toCharArray()) {
17+
if (c == '9') {
18+
continue;
19+
} else {
20+
numberToChangeToNine = Integer.parseInt("" + c);
21+
break;
22+
}
23+
}
24+
StringBuilder sb = new StringBuilder();
25+
sb.append('9');
26+
for (char c : remainder.toCharArray()) {
27+
if (Integer.parseInt(c + "") == numberToChangeToNine) {
28+
sb.append("9");
29+
} else {
30+
sb.append(c);
31+
}
32+
}
33+
return Integer.parseInt(sb.toString());
34+
} else {
35+
int numberToChangeToNine = firstDigit;
36+
StringBuilder sb = new StringBuilder();
37+
for (char c : Integer.toString(num).toCharArray()) {
38+
if (Integer.parseInt("" + c) == numberToChangeToNine) {
39+
sb.append("9");
40+
} else {
41+
sb.append(c);
42+
}
43+
}
44+
return Integer.parseInt(sb.toString());
45+
}
46+
}
47+
48+
private int getSmallest(int num) {
49+
int firstDigit = Integer.parseInt(Integer.toString(num).substring(0, 1));
50+
if (firstDigit == 1) {
51+
String remainder = Integer.toString(num).substring(1);
52+
int numberToChangeToZero = 0;
53+
for (char c : remainder.toCharArray()) {
54+
if (c == '0' || c == '1') {
55+
continue;
56+
} else {
57+
numberToChangeToZero = Integer.parseInt("" + c);
58+
break;
59+
}
60+
}
61+
StringBuilder sb = new StringBuilder();
62+
sb.append('1');
63+
for (char c : remainder.toCharArray()) {
64+
if (Integer.parseInt(c + "") == numberToChangeToZero) {
65+
sb.append("0");
66+
} else {
67+
sb.append(c);
68+
}
69+
}
70+
return Integer.parseInt(sb.toString());
71+
} else {
72+
int numberToChangeToOne = firstDigit;
73+
StringBuilder sb = new StringBuilder();
74+
for (char c : Integer.toString(num).toCharArray()) {
75+
if (Integer.parseInt("" + c) == numberToChangeToOne) {
76+
sb.append("1");
77+
} else {
78+
sb.append(c);
79+
}
80+
}
81+
return Integer.parseInt(sb.toString());
82+
}
83+
}
84+
}
85+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1432;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1432Test {
10+
private static _1432.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1432.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(888, solution1.maxDiff(555));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)