Skip to content

Commit addbe74

Browse files
add 788
1 parent b038d8b commit addbe74

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|788|[Rotated Digits](https://leetcode.com/problems/rotated-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_788.java) | O(n*m) | O(1) | |Easy|
2526
|784|[Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_784.java) | O(n*2^n) | O(n*2^n) | |Easy|
2627
|783|[Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_783.java) | O(n) | O(h) | |Easy|
2728
|779|[K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_779.java) | O(logn) | O(1) | |Medium|
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
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+
*/
29+
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++;
44+
}
45+
}
46+
return count;
47+
}
48+
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));
57+
}
58+
}
59+
return !originalNum.equals(sb.toString());
60+
}
61+
}
62+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._788;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _788Test {
10+
private static _788.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _788.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.rotatedDigits(10));
20+
}
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy