题目标签:Backtracking
建立一个hashmap 把数字 对应 字母 存入 map;
利用dfs,每次存入一个 char,当 chars 达到 digtis 的size 返回,具体看code。
Java Solution:
Runtime: 0 ms, faster than 100.00 %
Memory Usage: 38.7 MB, less than 6.16 %
完成日期:12/15/2019
关键点:HashMap
class Solution { List<String> res; HashMap<Character, String> map; public List<String> letterCombinations(String digits) { res = new ArrayList<>(); map = new HashMap<>(); StringBuilder comb = new StringBuilder(); if(digits.length() == 0) return res; map.put('2', "abc"); map.put('3', "def"); map.put('4', "ghi"); map.put('5', "jkl"); map.put('6', "mno"); map.put('7', "pqrs"); map.put('8', "tuv"); map.put('9', "wxyz"); DFS(digits, 0, comb); return res; } private void DFS(String digits, int pos, StringBuilder comb) { if(pos == digits.length()) { res.add(comb.toString()); return; } // go through each char for(char c : map.get(digits.charAt(pos)).toCharArray()) { comb.append(c); DFS(digits, pos+1, comb); comb.deleteCharAt(comb.length()-1); } } }
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/