Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26ca794

Browse files
solves #246: Strobogrammatic Number in java
1 parent b4f3197 commit 26ca794

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

β€ŽREADME.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@
215215
| 243 | πŸ”’ [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance) | [![Java](assets/java.png)](src/ShortestWordDistance.java) | |
216216
| 244 | πŸ”’ [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii) | | |
217217
| 245 | πŸ”’ [Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii) | | |
218-
| 246 | πŸ”’ [Strobogramatic Number](https://leetcode.com/problems/strobogrammatic-number) | | |
219-
| 247 | πŸ”’ [Strobogramatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii) | | |
218+
| 246 | πŸ”’ [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number) | [![Java](assets/java.png)](src/StrobogrammaticNumber.java) | |
219+
| 247 | πŸ”’ [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii) | | |
220220
| 249 | πŸ”’ [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings) | | |
221221
| 250 | πŸ”’ [Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees) | | |
222222
| 251 | πŸ”’ [Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector) | | |

β€Žsrc/StrobogrammaticNumber.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://leetcode.com/problems/strobogrammatic-number
2+
// T: O(N)
3+
// S: O(1)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
// Strobogrammatic digits: 0, 1, 8
9+
// Strobogrammatic interchangeable digits: 6 --> 9, 9 --> 6
10+
public class StrobogrammaticNumber {
11+
private static final Map<Character, Character> STROBOGRAMMATIC_MIRROR = new HashMap<>() {{
12+
put('0', '0');
13+
put('1', '1');
14+
put('6', '9');
15+
put('8', '8');
16+
put('9', '6');
17+
}};
18+
19+
public boolean isStrobogrammatic(String number) {
20+
for (int i = 0 ; i < number.length() ; i++) {
21+
final char digit = number.charAt(i);
22+
if (!STROBOGRAMMATIC_MIRROR.containsKey(digit)
23+
|| STROBOGRAMMATIC_MIRROR.get(digit) != number.charAt(number.length() - 1 - i)
24+
) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /