​welcome to my blog​

LeetCode Top 100 Liked Questions 17. Letter Combinations of a Phone Number (Java版; Medium)

题目描述

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

LeetCode Top 100 Liked Questions 17. Letter Combinations of a Phone Number (Java版; Medium)_java

class Solution {

public List<String> letterCombinations(String digits) {
HashMap<Character, String> map = new HashMap<>();
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");

List<String> list = new ArrayList<>();
if(digits.length()==0){
return list;
}
StringBuilder sb = new StringBuilder();
core(map, list, sb, digits.toCharArray(), 0);
return list;
}

private void core(HashMap<Character, String> map, List<String> list, StringBuilder sb, char[] chs, int i){
//base case
if(i==chs.length){
list.add(sb.toString());
return;
}
//
char ch = chs[i];
for(char ch2 : map.get(ch).toCharArray()){
sb.append(ch2);
core(map, list, sb, chs, i+1);
sb.deleteCharAt(sb.length()-1);
}
}
}

第一次做,递归, 回溯法? 深度优先遍历? 基础不牢: 匿名内部类? String和char要严格区分, str.charAt()返回的是char, 不是String!!! 改变现场, 恢复现场

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if(digits==null||digits.length()==0)
return res;
//匿名内部类
HashMap<Character, String> hm = new HashMap<Character, String>(){
{
put('2',"abc");
put('3',"def");
put('4',"ghi");
put('5',"jkl");
put('6',"mno");
put('7',"pqrs");
put('8',"tuv");
put('9',"wxyz");
}
};
StringBuilder sb = new StringBuilder();
Core(digits, 0, sb, res, hm);
return res;
}
public void Core(String digits, int index, StringBuilder sb, List<String> res, HashMap<Character, String> hm){
if(index==digits.length()){
res.add(sb.toString());
return;
}
String curr = hm.get(digits.charAt(index));
for(int i=0; i<curr.length(); i++){
sb.append(curr.charAt(i));
Core(digits, index+1, sb, res, hm);
//restore
sb.deleteCharAt(sb.length()-1);
}
}
}

匿名内部类

LeetCode Top 100 Liked Questions 17. Letter Combinations of a Phone Number (Java版; Medium)_List_02

利用了队列的优秀答案

public List<String> letterCombinations(String digits) {
LinkedList<String> ans = new LinkedList<String>();
if(digits.isEmpty()) return ans;
String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ans.add("");
for(int i =0; i<digits.length();i++){
int x = Character.getNumericValue(digits.charAt(i));
while(ans.peek().length()==i){
String t = ans.remove();
for(char s : mapping[x].toCharArray())
ans.add(t+s);
}
}
return ans;
}