给定n,返回所有长度为n的频闪数字。
- 时间复杂度 O(n),空间复杂度O(n)
- Runtime: 140 ms, faster than 95.92%
- Memory Usage: 61.2 MB, less than 61.90%
/**
* @param {number} n
* @return {string[]}
*/
var findStrobogrammatic = function(n) {
const helper = (n, m) => {
if (n === 0) return [''];
if (n === 1) return ['0', '1', '8'];
const list = helper(n - 2, m);
const res = [];
for (let i = 0; i < list.length; i++) {
const s = list[i];
if (n !== m) res.push('0' + s + '0');
res.push('1' + s + '1');
res.push('6' + s + '9');
res.push('8' + s + '8');
res.push('9' + s + '6');
}
return res;
}
return helper(n, n);
};
网友评论