-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0247-strobogrammatic-number-ii.js
More file actions
43 lines (38 loc) · 1.04 KB
/
0247-strobogrammatic-number-ii.js
File metadata and controls
43 lines (38 loc) · 1.04 KB
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
/**
* 247. Strobogrammatic Number II
* https://leetcode.com/problems/strobogrammatic-number-ii/
* Difficulty: Medium
*
* Given an integer n, return all the strobogrammatic numbers that are of length n. You may
* return the answer in any order.
*
* A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked
* at upside down).
*/
/**
* @param {number} n
* @return {string[]}
*/
var findStrobogrammatic = function(n) {
const pairs = [
['0', '0'],
['1', '1'],
['6', '9'],
['8', '8'],
['9', '6']
];
return generateStrobogrammatic(n, n);
function generateStrobogrammatic(length, finalLength) {
if (length === 0) return [''];
if (length === 1) return ['0', '1', '8'];
const result = [];
const subNumbers = generateStrobogrammatic(length - 2, finalLength);
for (const [left, right] of pairs) {
for (const sub of subNumbers) {
if (length === finalLength && left === '0') continue;
result.push(left + sub + right);
}
}
return result;
}
};