From 7cbf0b1fff9675d507a686f4de7c08ef0c3d1394 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 5 Jun 2021 18:40:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 33 ++++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index fa135d8d..aefee698 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -137,7 +137,7 @@ for (int i = 0; i < letters.size(); i++) { 关键地方都讲完了,按照[关于回溯算法,你该了解这些!](https://mp.weixin.qq.com/s/gjSgJbNbd1eAA5WkA-HeWw)中的回溯法模板,不难写出如下C++代码: -``` +```c++ // 版本一 class Solution { private: @@ -183,7 +183,7 @@ public: 一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方) -``` +```c++ // 版本二 class Solution { private: @@ -319,7 +319,7 @@ class Solution: python3: -```python3 +```py class Solution: def letterCombinations(self, digits: str) -> List[str]: self.s = "" @@ -342,6 +342,33 @@ class Solution: Go: +javaScript: + +```js +var letterCombinations = function(digits) { + const k = digits.length; + const map = ["","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]; + if(!k) return []; + if(k === 1) return map[digits].split(""); + + const res = [], path = []; + backtracking(digits, k, 0); + return res; + + function backtracking(n, k, a) { + if(path.length === k) { + res.push(path.join("")); + return; + } + for(const v of map[n[a]]) { + path.push(v); + backtracking(n, k, a + 1); + path.pop(); + } + + } +}; +```