Skip to content

Commit 2ddc221

Browse files
committed
add 246 java
1 parent de1c9a5 commit 2ddc221

File tree

6 files changed

+286
-7
lines changed

6 files changed

+286
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,8 @@
956956
199| [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) | [Python](./leetcode_python/Depth-First-Search/binary-tree-right-side-view.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/BinaryTreeRightSideView.java)| _O(n)_ | _O(h)_ | Medium |`good basic`, `dfs`, `bfs`,`apple`,`amazon`, `fb`, LC 102| OK*** (6)
957957
200| [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Python](./leetcode_python/Depth-First-Search/number-of-islands.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/NumberOfIslands.java) | _O(m * n)_ | _O(m * n)_| Medium |Curated Top 75, `bfs`, `dfs`,`good basic`,check `# 694, 711 Number of Distinct Islands`,`apple`, `Goldman Sachs`,`google`,`amazon`,`fb`| OK****** (6)
958958
236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | [Python](./leetcode_python/Depth-First-Search/lowest-common-ancestor-of-a-binary-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/LowestCommonAncestorOfABinaryTree.java) | _O(n)_ | _O(h)_ | Medium | `trick`,EPI, `LCA`,check `235 Lowest Common Ancestor of a Binary Search Tree`,`apple`,`amazon`,`linkedin`,`fb` | AGAIN************* (9)
959-
247| [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/) | [Python](./leetcode_python/Depth-First-Search/strobogrammatic-number-ii.py) | _O(n^2 * 5^(n/2))_ | _O(n)_ | Medium |🔒,check `#246 Strobogrammatic Number`,`good trick`, `google`,`fb`| AGAIN******** (4)
959+
247| [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/) | [Python](./leetcode_python/Depth-First-Search/strobogrammatic-number-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/StrobogrammaticNumber2.java)
960+
| _O(n^2 * 5^(n/2))_ | _O(n)_ | Medium |🔒,check `#246 Strobogrammatic Number`,`good trick`, `google`,`fb`| AGAIN******** (5)
960961
250| [Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees) | [Python](./leetcode_python/Depth-First-Search/count-univalue-subtrees.py) | _O(n)_ | _O(h)_ | Medium |🔒| AGAIN (not start)
961962
257| [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) | [Python](./leetcode_python/Depth-First-Search/binary-tree-paths.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/BinaryTreePaths.java) | _O(n * h)_ | _O(h)_ | Easy | good basic,`apple`, `google`, `amazon`, `fb`, iterative, dfs, LC 112, 113| AGAIN********* (MUST) (7)
962963
332| [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | [Python](./leetcode_python/Depth-First-Search/reconstruct-itinerary.py) | _O(t! / (n1! * n2! * ... nk!))_ | _O(t)_ | Medium |`yelp`, `google`| AGAIN* (3) (not start)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2025-01-07
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2025-01-05
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20250107: 247
12
20250105: 399,1091
23
20250104: 1031,369
34
20250101: 729,731

data/to_review.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-03-03 -> ['247']
12
2025-03-01 -> ['399,1091']
23
2025-02-28 -> ['1031,369']
34
2025-02-25 -> ['729,731']
@@ -8,32 +9,34 @@
89
2025-02-15 -> ['369,311']
910
2025-02-14 -> ['370']
1011
2025-02-13 -> ['815,871,593,1109']
12+
2025-02-10 -> ['247']
1113
2025-02-08 -> ['399,1091']
1214
2025-02-07 -> ['1031,369', '560,523']
1315
2025-02-04 -> ['729,731']
1416
2025-02-03 -> ['769,817,855(again)']
1517
2025-02-01 -> ['498(again),934', '304,853,325']
1618
2025-01-31 -> ['379,173']
1719
2025-01-30 -> ['079,212(todo),362,849']
20+
2025-01-28 -> ['247']
1821
2025-01-26 -> ['399,1091', '370(todo)']
1922
2025-01-25 -> ['1031,369', '369,311']
2023
2025-01-24 -> ['370', '34,767']
2124
2025-01-23 -> ['815,871,593,1109']
2225
2025-01-22 -> ['729,731']
2326
2025-01-21 -> ['769,817,855(again)']
24-
2025-01-20 -> ['722,380']
27+
2025-01-20 -> ['247', '722,380']
2528
2025-01-19 -> ['498(again),934', '33,81']
2629
2025-01-18 -> ['399,1091', '379,173']
2730
2025-01-17 -> ['1031,369', '079,212(todo),362,849', '560,523', '253']
2831
2025-01-16 -> ['776,31']
29-
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
32+
2025-01-15 -> ['247', '004(todo),34(todo),162(todo),275(todo)']
3033
2025-01-14 -> ['729,731', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
3134
2025-01-13 -> ['399,1091', '769,817,855(again)']
32-
2025-01-12 -> ['1031,369', '369,311']
35+
2025-01-12 -> ['247', '1031,369', '369,311']
3336
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
34-
2025-01-10 -> ['399,1091', '379,173', '815,871,593,1109', '833,950']
35-
2025-01-09 -> ['1031,369', '729,731', '079,212(todo),362,849']
36-
2025-01-08 -> ['399,1091', '769,817,855(again)']
37+
2025-01-10 -> ['247', '399,1091', '379,173', '815,871,593,1109', '833,950']
38+
2025-01-09 -> ['247', '1031,369', '729,731', '079,212(todo),362,849']
39+
2025-01-08 -> ['247', '399,1091', '769,817,855(again)']
3740
2025-01-07 -> ['399,1091', '1031,369']
3841
2025-01-06 -> ['399,1091', '1031,369', '729,731', '498(again),934']
3942
2025-01-05 -> ['1031,369', '769,817,855(again)', '379,173', '370(todo)']
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package LeetCodeJava.DFS;
2+
3+
// https://leetcode.com/problems/strobogrammatic-number-ii/description/
4+
// https://leetcode.ca/all/247.html
5+
6+
import java.util.*;
7+
8+
/**
9+
* 247. Strobogrammatic Number II
10+
* A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
11+
*
12+
* Find all strobogrammatic numbers that are of length = n.
13+
*
14+
* Example:
15+
*
16+
* Input: n = 2
17+
* Output: ["11","69","88","96"]
18+
* Difficulty:
19+
* Medium
20+
* Lock:
21+
* Prime
22+
* Company:
23+
* Cisco Facebook Google
24+
* Problem Solution
25+
* 247-Strobogrammatic-Number-II
26+
*
27+
*/
28+
public class StrobogrammaticNumber2 {
29+
30+
// V0
31+
// TODO: validate and fix
32+
// List<String> res = new ArrayList<>();
33+
// public List<String> findStrobogrammatic_1_1(int n) {
34+
// for(int i = 0; i < n; i++){
35+
// HashSet<String> cache = new HashSet<>();
36+
// StringBuilder sb = new StringBuilder();
37+
// findNumbers(n, sb, cache);
38+
// }
39+
// return res;
40+
// }
41+
//
42+
// private void findNumbers(int n, StringBuilder sb, Set<String> cache){
43+
//
44+
// String[] digits = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
45+
// String str = sb.toString();
46+
// if (str.length() == n){
47+
// if (isStrobogrammatic(str)){
48+
// if(!cache.contains(str)){
49+
// cache.add(str);
50+
// res.add(str);
51+
// }
52+
// }
53+
// return;
54+
// }
55+
//
56+
// if (str.length() > n){
57+
// return;
58+
// }
59+
//
60+
// for (String x: digits){
61+
// findNumbers(n, sb.append(x), cache);
62+
// }
63+
// }
64+
//
65+
// private boolean isStrobogrammatic(String x){
66+
// Map<String, String> symmertricMapping = new HashMap<>();
67+
// symmertricMapping.put("0", "0");
68+
// symmertricMapping.put("1", "1");
69+
// symmertricMapping.put("8", "8");
70+
// symmertricMapping.put("6", "9");
71+
// symmertricMapping.put("9", "6");
72+
// int l = 0;
73+
// int r = x.length() - 1;
74+
// while (r > l){
75+
//// if (x.charAt(l) != x.charAt(r)){
76+
//// return false;
77+
//// }
78+
// String left = String.valueOf(x.charAt(l));
79+
// String right = String.valueOf(x.charAt(r));
80+
// if (!symmertricMapping.get(left).equals(right)){
81+
// return false;
82+
// }
83+
// r -= 1;
84+
// l += 1;
85+
// }
86+
//
87+
// return true;
88+
// }
89+
90+
// V1-1
91+
// https://leetcode.ca/2016-08-03-247-Strobogrammatic-Number-II/
92+
List<String> singleDigitList = new ArrayList<>(Arrays.asList("0", "1", "8")); // not char[], because List can direct return as result
93+
char[][] digitPair = { {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'} }; // except '0', a special case
94+
95+
public List<String> findStrobogrammatic_1_1(int n) {
96+
return dfs(n, n);
97+
}
98+
99+
public List<String> dfs(int k, int n) {
100+
if (k <= 0) {
101+
return new ArrayList<String>(Arrays.asList(""));
102+
}
103+
if (k == 1) {
104+
return singleDigitList;
105+
}
106+
107+
List<String> subList = dfs(k - 2, n);
108+
List<String> result = new ArrayList<>();
109+
110+
for (String str : subList) {
111+
if (k != n) { // @note: cannot start with 0
112+
result.add("0" + str + "0");
113+
}
114+
for (char[] aDigitPair : digitPair) {
115+
result.add(aDigitPair[0] + str + aDigitPair[1]);
116+
}
117+
}
118+
119+
return result;
120+
}
121+
122+
// V1-2
123+
// https://leetcode.ca/2016-08-03-247-Strobogrammatic-Number-II/
124+
private static final int[][] PAIRS = { {1, 1}, {8, 8}, {6, 9}, {9, 6}};
125+
private int n;
126+
127+
public List<String> findStrobogrammatic_1_2(int n) {
128+
this.n = n;
129+
return dfs(n);
130+
}
131+
132+
private List<String> dfs(int u) {
133+
if (u == 0) {
134+
return Collections.singletonList("");
135+
}
136+
if (u == 1) {
137+
return Arrays.asList("0", "1", "8");
138+
}
139+
List<String> ans = new ArrayList<>();
140+
for (String v : dfs(u - 2)) {
141+
for ( int[] p : PAIRS) {
142+
ans.add(p[0] + v + p[1]);
143+
}
144+
if (u != n) {
145+
ans.add(0 + v + 0);
146+
}
147+
}
148+
return ans;
149+
}
150+
151+
// V2-1
152+
// IDEA : DFS (gpt)
153+
// TODO: validate the code
154+
List<String> res = new ArrayList<>();
155+
public List<String> findStrobogrammatic_2_1(int n) {
156+
// Use valid strobogrammatic pairs only
157+
char[][] pairs = new char[][] {
158+
{'0', '0'}, {'1', '1'}, {'8', '8'},
159+
{'6', '9'}, {'9', '6'}
160+
};
161+
162+
findNumbers(n, new char[n], 0, n - 1, pairs);
163+
return res;
164+
}
165+
166+
private void findNumbers(int n, char[] current, int left, int right, char[][] pairs) {
167+
if (left > right) {
168+
// Base case: Valid strobogrammatic number
169+
res.add(new String(current));
170+
return;
171+
}
172+
173+
for (char[] pair : pairs) {
174+
// Avoid leading zero unless the number is of length 1
175+
if (left == 0 && pair[0] == '0' && n > 1) {
176+
continue;
177+
}
178+
179+
// Place the pair
180+
current[left] = pair[0];
181+
current[right] = pair[1];
182+
183+
// Recursive call
184+
findNumbers(n, current, left + 1, right - 1, pairs);
185+
}
186+
}
187+
188+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,4 +2161,87 @@ public int shortestPathBinaryMatrix(int[][] grid) {
21612161
// return curLen != 0 ? res : -1;
21622162
// }
21632163

2164+
// LC 247
2165+
// https://leetcode.ca/2016-08-03-247-Strobogrammatic-Number-II/
2166+
// 9.57 pm - 10:15 pm
2167+
/**
2168+
* strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
2169+
*
2170+
* -> Find all strobogrammatic numbers that are of `length = n.`
2171+
*
2172+
* -> exp 1: n=2, ["11","69","88","96"]
2173+
* -> exp 2: n=1, ["0","1","8"]
2174+
* -> exp 3: n=3, ["101","111","181","609","619","689","808","818","888","906","916","986"]
2175+
* -> exp 4 : n=4, ["1001","1111","1691","1881","1961","6009","6119","6699","6889","6969","8008","8118","8698","8888","8968","9006","9116","9696","9886","9966"]
2176+
* ...
2177+
*
2178+
*/
2179+
/**
2180+
* IDEA 1) DFS
2181+
*
2182+
* -> for n = n
2183+
* -> check 0 -> 9, if is strobogrammatic number, add to list
2184+
* -> repeat ...
2185+
*
2186+
*
2187+
*/
2188+
List<String> res = new ArrayList<>();
2189+
2190+
public List<String> findStrobogrammatic_1_1(int n) {
2191+
for(int i = 0; i < n; i++){
2192+
HashSet<String> cache = new HashSet<>();
2193+
StringBuilder sb = new StringBuilder();
2194+
findNumbers(n, sb, cache);
2195+
}
2196+
return res;
2197+
}
2198+
2199+
private void findNumbers(int n, StringBuilder sb, Set<String> cache){
2200+
2201+
String[] digits = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
2202+
String str = sb.toString();
2203+
if (str.length() == n){
2204+
if (isStrobogrammatic(str)){
2205+
if(!cache.contains(str)){
2206+
cache.add(str);
2207+
res.add(str);
2208+
}
2209+
}
2210+
return;
2211+
}
2212+
2213+
if (str.length() > n){
2214+
return;
2215+
}
2216+
2217+
for (String x: digits){
2218+
findNumbers(n, sb.append(x), cache);
2219+
}
2220+
}
2221+
2222+
private boolean isStrobogrammatic(String x){
2223+
Map<String, String> symmertricMapping = new HashMap<>();
2224+
symmertricMapping.put("0", "0");
2225+
symmertricMapping.put("1", "1");
2226+
symmertricMapping.put("8", "8");
2227+
symmertricMapping.put("6", "9");
2228+
symmertricMapping.put("9", "6");
2229+
int l = 0;
2230+
int r = x.length() - 1;
2231+
while (r > l){
2232+
// if (x.charAt(l) != x.charAt(r)){
2233+
// return false;
2234+
// }
2235+
String left = String.valueOf(x.charAt(l));
2236+
String right = String.valueOf(x.charAt(r));
2237+
if (!symmertricMapping.get(left).equals(right)){
2238+
return false;
2239+
}
2240+
r -= 1;
2241+
l += 1;
2242+
}
2243+
2244+
return true;
2245+
}
2246+
21642247
}

0 commit comments

Comments
 (0)