-
Notifications
You must be signed in to change notification settings - Fork 2
/
FindStrobogrammatic.java
67 lines (61 loc) · 1.98 KB
/
FindStrobogrammatic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package logic;
import java.util.ArrayList;
import java.util.List;
/**
* 247. Strobogrammatic Number II
*
* A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Find all
* strobogrammatic numbers that are of length = n.
*
* Example:
*
* Input: n = 2
* Output: ["11","69","88","96"]
*
* IMP-1: This is a very clever solution - should practice
*/
public class FindStrobogrammatic {
//single numbers that are symmetric
char[] odd = {'1', '8', '0'};
//pairs that are symmetric at 180 degree
char[][] even = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
List<String> retList = new ArrayList<>();
public static void main(String[] args) {
FindStrobogrammatic fstr = new FindStrobogrammatic();
fstr.findStrobogrammatic(3);
System.out.println(fstr.retList);
}
public List<String> findStrobogrammatic(int n) {
char[] buffer = new char[n];
processChars(buffer, 0, n - 1);
return retList;
}
/**
* recurse through the combinations to find all that would work. this is again a real simple solution but coming up
* fresh with it is very tricky! someone else at leetcode came up with below in the discussion....
*
* @param buffer
* @param left
* @param right
*/
void processChars(char[] buffer, int left, int right) {
if (left > right) {
retList.add(new String(buffer));
return;
}
if (left == right) {
for (Character ch : odd) {
buffer[left] = ch;
processChars(buffer, left + 1, right - 1);
}
} else {
for (char[] ch : even) {
//skip numbers that would start with 0
if (ch[0] == '0' && left == 0) continue;
buffer[left] = ch[0];
buffer[right] = ch[1];
processChars(buffer, left + 1, right - 1);
}
}
}
}