Letter Combinations of a Phone Number || 电话号码对应的单词

来源:互联网 发布:通达信选股软件下载 编辑:程序博客网 时间:2024/05/21 10:47

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"].

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

public class Solution {    public String []c={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};    public int []total={0,0,3,3,3,3,3,4,3,4};    public List<String> letterCombinations(String digits) {        List<String> res=new LinkedList<String>();        dfs(digits,"",0,res);        return res;    }    public void dfs(String digits,String ans,int index,List<String> res){        if(index==digits.length()){            res.add(ans);            return;        }        int i=digits.charAt(index)-'0';        if(i<2 || i>10) return;        for(int j=0;j<total[i];j++){            dfs(digits,ans+String.valueOf(c[i].charAt(j)),index+1,res);        }    }}


0 0
原创粉丝点击