美文网首页
247. Strobogrammatic Number II

247. Strobogrammatic Number II

作者: jluemmmm | 来源:发表于2021-10-18 00:38 被阅读0次

    给定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);
    };
    
    

    相关文章

      网友评论

          本文标题:247. Strobogrammatic Number II

          本文链接:https://www.haomeiwen.com/subject/lwqgoltx.html