-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path247. Strobogrammatic-Number-II
92 lines (76 loc) · 2.16 KB
/
247. Strobogrammatic-Number-II
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
What is Strobogrammatic Number?
1. Solve the Strobogrammatic-Number first:
class Solution:
"""
@param num: a string
@return: true if a number is strobogrammatic or false
"""
def is_strobogrammatic(self, num: str) -> bool:
# write your code here
flipped = [0,1,-1,-1,-1,-1,9,-1,8,6]
i = 0
j = len(num) - 1
while i <= j:
if flipped[int(num[i])] != int(num[j]):
return False
i += 1
j -= 1
return True
2. Solve Strobogrammatic-Number-II
export class Solution {
/**
* @param n: the length of strobogrammatic number
* @return: All strobogrammatic numbers
* we will sort your return value in output
*/
findStrobogrammatic(n) {
// write your code here
let validPairs = [['6','9'],['9','6'],['8','8'],['1','1'],['0','0']]
function backtrack(n) {
if (n == 0) {
return [""];
}
if (n == 1) {
return ["0","1","8"];
}
if (n == 2) {
return ["11","69","96","88","00"]
}
let prevSeq = backtrack(n-2);
let currentRes = []
for (let seq of prevSeq) {
for (let [a,b] of validPairs) {
currentRes.push(a + seq + b);
}
}
return currentRes;
}
let res = backtrack(n).filter(el => el.length === 1 || el[0] !== '0');
return res
}
}
from typing import (
List,
)
class Solution:
"""
@param n: the length of strobogrammatic number
@return: All strobogrammatic numbers
we will sort your return value in output
"""
def find_strobogrammatic(self, n: int) -> List[str]:
# write your code here
def dfs(depth):
if depth == 0:
return ['']
if depth == 1:
return ['0','1','8']
subResults = dfs(depth - 2)
res = []
for subResult in subResults:
for left, right in ('11','88','69','96'):
res.append(left + subResult + right)
if depth != n:
res.append('0' + subResult + '0')
return res
return dfs(n)