Skip to content

Commit c9d2ec2

Browse files
refactor 788
1 parent 5bb0c04 commit c9d2ec2

File tree

1 file changed

+28
-51
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-51
lines changed

src/main/java/com/fishercoder/solutions/_788.java

+28-51
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,37 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6-
/**
7-
* 788. Rotated Digits
8-
*
9-
* X is a good number if after rotating each digit individually by 180 degrees,
10-
* we get a valid number that is different from X.
11-
* A number is valid if each digit remains a digit after rotation.
12-
* 0, 1, and 8 rotate to themselves;
13-
* 2 and 5 rotate to each other;
14-
* 6 and 9 rotate to each other,
15-
* and the rest of the numbers do not rotate to any other number.
16-
17-
Now given a positive number N, how many numbers X from 1 to N are good?
18-
19-
Example:
20-
Input: 10
21-
Output: 4
22-
23-
Explanation:
24-
There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
25-
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
26-
27-
Note: N will be in range [1, 10000].
28-
*/
296
public class _788 {
30-
public static class Solution1 {
31-
public int rotatedDigits(int N) {
32-
int count = 0;
33-
Map<Character, String> map = new HashMap<>();
34-
map.put('0', "0");
35-
map.put('1', "1");
36-
map.put('8', "8");
37-
map.put('2', "5");
38-
map.put('5', "2");
39-
map.put('6', "9");
40-
map.put('9', "6");
41-
for (int i = 1; i <= N; i++) {
42-
if (isRotatedNumber(i, map)) {
43-
count++;
7+
public static class Solution1 {
8+
public int rotatedDigits(int N) {
9+
int count = 0;
10+
Map<Character, String> map = new HashMap<>();
11+
map.put('0', "0");
12+
map.put('1', "1");
13+
map.put('8', "8");
14+
map.put('2', "5");
15+
map.put('5', "2");
16+
map.put('6', "9");
17+
map.put('9', "6");
18+
for (int i = 1; i <= N; i++) {
19+
if (isRotatedNumber(i, map)) {
20+
count++;
21+
}
22+
}
23+
return count;
4424
}
45-
}
46-
return count;
47-
}
4825

49-
private boolean isRotatedNumber(int num, Map<Character, String> map) {
50-
String originalNum = String.valueOf(num);
51-
StringBuilder sb = new StringBuilder();
52-
for (char c : String.valueOf(num).toCharArray()) {
53-
if (!map.containsKey(c)) {
54-
return false;
55-
} else {
56-
sb.append(map.get(c));
26+
private boolean isRotatedNumber(int num, Map<Character, String> map) {
27+
String originalNum = String.valueOf(num);
28+
StringBuilder sb = new StringBuilder();
29+
for (char c : String.valueOf(num).toCharArray()) {
30+
if (!map.containsKey(c)) {
31+
return false;
32+
} else {
33+
sb.append(map.get(c));
34+
}
35+
}
36+
return !originalNum.equals(sb.toString());
5737
}
58-
}
59-
return !originalNum.equals(sb.toString());
6038
}
61-
}
6239
}

0 commit comments

Comments
 (0)