Skip to content

Commit 1168932

Browse files
edit 248
1 parent 27dccf4 commit 1168932

File tree

3 files changed

+114
-69
lines changed

3 files changed

+114
-69
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ Your ideas/fixes/algorithms are more than welcome!
289289
|251|[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)|[Solution](../master/src/main/java/com/fishercoder/solutions/Flatten2DVector.java)| O(1)|O(m*n) | Medium|
290290
|250|[Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/CountUnivalueSubtrees.java)| O(n)|O(h) | Medium| DFS
291291
|249|[Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/GroupShiftedStrings.java) | O(nlogn) | O(n) |
292-
|248|[Strobogrammatic Number III](https://leetcode.com/problems/strobogrammatic-number-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/StrobogrammaticNumberIII.java) | O(?) | O(?) | Hard |
292+
|248|[Strobogrammatic Number III](https://leetcode.com/problems/strobogrammatic-number-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_248.java) | O(?) | O(?) | Hard | Recursion, DFS
293293
|247|[Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/StrobogrammaticNumberII.java) | O(n^2) | O(n) | Medium | Recursion
294294
|246|[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/StrobogrammaticNumber.java) | O(n) | O(1) | Easy
295295
|245|[Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ShortestWordDistanceIII.java) | O(n) | O(1) | Medium |

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

-68
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
8+
9+
Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.
10+
11+
For example,
12+
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
13+
14+
Note:
15+
Because the range might be a large number, the low and high numbers are represented as string.
16+
17+
*/
18+
public class _248 {
19+
20+
static class Solution2 {
21+
/**Credit: https://discuss.leetcode.com/topic/31386/concise-java-solution
22+
*
23+
Construct char arrays from low.length() to high.length()
24+
Add stro pairs from outside
25+
When left > right, add eligible count
26+
*/
27+
28+
private static final char[][] pairs = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
29+
30+
public int strobogrammaticInRange(String low, String high) {
31+
int[] count = {0};
32+
for (int len = low.length(); len <= high.length(); len++) {
33+
char[] c = new char[len];
34+
dfs(low, high, c, 0, len - 1, count);
35+
}
36+
return count[0];
37+
}
38+
39+
public void dfs(String low, String high , char[] c, int left, int right, int[] count) {
40+
if (left > right) {
41+
String s = new String(c);
42+
if ((s.length() == low.length() && s.compareTo(low) < 0) ||
43+
(s.length() == high.length() && s.compareTo(high) > 0)) {
44+
return;
45+
}
46+
count[0]++;
47+
return;
48+
}
49+
for (char[] p : pairs) {
50+
c[left] = p[0];
51+
c[right] = p[1];
52+
if (c.length != 1 && c[0] == '0') {
53+
continue;
54+
}
55+
if (left == right && p[0] != p[1]) {
56+
continue;
57+
}
58+
dfs(low, high, c, left + 1, right - 1, count);
59+
}
60+
}
61+
}
62+
63+
class Solution1 {
64+
Map<Character, Character> map = new HashMap<>();
65+
66+
{
67+
map.put('1', '1');
68+
map.put('8', '8');
69+
map.put('6', '9');
70+
map.put('9', '6');
71+
map.put('0', '0');
72+
}
73+
74+
String low = "", high = "";
75+
76+
public int strobogrammaticInRange(String low, String high) {
77+
this.low = low;
78+
this.high = high;
79+
int result = 0;
80+
for (int n = low.length(); n <= high.length(); n++) {
81+
int[] count = new int[1];
82+
strobogrammaticInRange(new char[n], count, 0, n - 1);
83+
result += count[0];
84+
}
85+
return result;
86+
}
87+
88+
private void strobogrammaticInRange(char[] arr, int[] count, int lo, int hi) {
89+
if (lo > hi) {
90+
String s = new String(arr);
91+
if ((arr[0] != '0' || arr.length == 1) && compare(low, s) && compare(s, high)) {
92+
count[0]++;
93+
}
94+
return;
95+
}
96+
for (Character c : map.keySet()) {
97+
arr[lo] = c;
98+
arr[hi] = map.get(c);
99+
if ((lo == hi && c == map.get(c)) || lo < hi)
100+
strobogrammaticInRange(arr, count, lo + 1, hi - 1);
101+
}
102+
}
103+
104+
private boolean compare(String a, String b) {
105+
if (a.length() != b.length())
106+
return a.length() < b.length();
107+
int i = 0;
108+
while (i < a.length() && a.charAt(i) == b.charAt(i))
109+
i++;
110+
return i == a.length() ? true : a.charAt(i) <= b.charAt(i);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)