Given a digit string, 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.
Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
public class Solution { public ListletterCombinations(String digits) { List result=new ArrayList (); if(digits==null || digits.length()==0){ return result; } List list=new ArrayList (); for(int i=0; i inputs, int step,StringBuilder sb,List res){ if(step==inputs.size()){ res.add(sb.toString()); return; } String str=inputs.get(step); for(int i=0; i
二刷,也可以用BFS来写:
class Solution { public ListletterCombinations(String digits) { List res = new ArrayList<>(); if(digits == null || digits.length() == 0){ return res; } String[] strArr = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; //int len = digits.length(); for(char c : digits.toCharArray()){ List temp = new ArrayList<>(); for(char ac : strArr[c - '0'].toCharArray()){ if(res.isEmpty()){ temp.add(""+ac); } else{ for(String str : res){ StringBuilder sb = new StringBuilder(str); sb.append(ac); temp.add(sb.toString()); } } } res = temp; } return res; } }