【问题标题】:Letter combinations of a phone keypad key电话键盘键的字母组合
【发布时间】:2018-12-19 05:54:49
【问题描述】:

我有一个关于 JavaScript 中电话键盘键的字母组合的问题。我使用 DFS 递归编写了一个解决方案。但它没有按预期工作。我是 JavaScript 新手,但用 Ruby 编写的代码类似。

问题在于从电话键盘获取所有可能的字母组合。

输入:“23”

输出:[“ad”、“ae”、“af”、“bd”、“be”、“bf”、“cd”、“ce”、“cf”]。

使用下面的代码,它会在“af”处停止。输出为 ["ad", "ae", "af"]。我不知道为什么这段代码没有移动到“2”的第二个字母,即“b”。

const map = {
  "2": ["a", "b", "c"],
  "3": ["d", "e", "f"],
  "4": ["g", "h", "i"],
  "5": ["j", "k", "l"],
  "6": ["m", "n", "o"],
  "7": ["p", "q", "r", "s"],
  "8": ["t", "u", "v"],
  "9": ["w", "x", "y", "z"]
};

let result = [];

let letterCombinations = function(digits) {
  if (digits.length == 0) {
    return []
  };

  let stack = [];
  dfs(digits.split(''), 0, stack)

  return result
};

function dfs(digits, index, stack) {
  const currentLetters = map[digits[index]]

  for (i = 0; i < currentLetters.length; i++) {
    stack.push(currentLetters[i])

    if (index == digits.length - 1) {
      result.push(stack.join(''))
      stack.pop()
    } else {
      dfs(digits, index + 1, stack)
      stack.pop()
    }
  }
}

console.log(letterCombinations("23"));

【问题讨论】:

    标签: javascript recursion microsoft-distributed-file-system


    【解决方案1】:

    你需要在你的 for 循环中声明 i 否则它是全局的并且在每个递归步骤中不断增加。

    使用for (let i = 0; i &lt; currentLetters.length; i++)

    const map = {
      "2": ["a", "b", "c"],
      "3": ["d", "e", "f"],
      "4": ["g", "h", "i"],
      "5": ["j", "k", "l"],
      "6": ["m", "n", "o"],
      "7": ["p", "q", "r", "s"],
      "8": ["t", "u", "v"],
      "9": ["w", "x", "y", "z"]
    };
    
    let result = [];
    
    let letterCombinations = function(digits) {
      if (digits.length == 0) {
        return []
      };
    
      let stack = [];
      dfs(digits.split(''), 0, stack)
    
      return result
    };
    
    function dfs(digits, index, stack) {
      const currentLetters = map[digits[index]]
      
      // declare the loop variable!
      for (let i = 0; i < currentLetters.length; i++) {
        stack.push(currentLetters[i])
    
        if (index == digits.length - 1) {
          result.push(stack.join(''))
          stack.pop()
        } else {
          dfs(digits, index + 1, stack)
          stack.pop()
        }
      }
    }
    
    console.log(letterCombinations("23"));

    【讨论】:

    • 哇,非常感谢!在 JS 中编写 for 循环时,声明 i 几乎是常态吗?
    • 是的,@YoungsunPaik,你几乎总是想声明它,这样你就不会污染全局空间,也不要在循环之间共享它。
    【解决方案2】:

    这是一个不太复杂的实现。希望对您有用!

    const map = {
      "2": ["a", "b", "c"],
      "3": ["d", "e", "f"],
      "4": ["g", "h", "i"],
      "5": ["j", "k", "l"],
      "6": ["m", "n", "o"],
      "7": ["p", "q", "r", "s"],
      "8": ["t", "u", "v"],
      "9": ["w", "x", "y", "z"]
    };
    
    function letterCombinations(digits) {
      digits = digits.split('');
      
      const firstArray = map[digits[0]];
      const secondArray = map[digits[1]];
      const result = [];
      
      for (let i = 0; i < firstArray.length; i++)
      {
        for (let j = 0; j < secondArray.length; j++)
        {
          result.push(firstArray[i] + secondArray[j]);
        }
      }
      
      return result;
    };
    
    console.log(letterCombinations("23"));

    【讨论】:

    • 它不那么复杂,因为与 OP 的代码不同,它只适用于输入正好两个数字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    相关资源
    最近更新 更多