电话号码的字母组合-17


力扣(LeetCode) 链接:https://leetcode.cn/problems/letter-combinations-of-a-phone-number/

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function (digits) {
  const map = {
    2: "abc",
    3: "def",
    4: "ghi",
    5: "jkl",
    6: "mno",
    7: "pqrs",
    8: "tuv",
    9: "wxyz",
  }

  const res = []
  const path = []
  if (digits.length === 0) return res
  function bfs(start) {
    if (path.length === digits.length) {
      res.push(path.join(""))
      return
    }
    const curStr = map[digits[start]]
    for (let i = 0; i < curStr.length; i++) {
      path.push(curStr[i])
      bfs(start + 1)
      path.pop()
    }
  }
  bfs(0)

  return res
}

文章作者: 高红翔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 高红翔 !
  目录