Leetcode--17.电话号码的字母组合
給定一個僅包含數字?2-9?的字符串,返回所有它能表示的字母組合。
給出數字到字母的映射如下(與電話按鍵相同)。注意 1 不對應任何字母。
示例:
輸入:"23"
輸出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
思路:
回溯法
提交的代碼:
class?Solution?{
????Map<String,?String>?phone?=?new?HashMap<String,?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");
??}};
????List<String>?output?=?new?ArrayList<String>();
????public?List<String>?letterCombinations(String?digits)?{
????????if(digits.length()!=0)
????????{
????????????back("",digits);
????????}
????????return?output;
????}
????public?void?back(String?now,String?next_word)
????{
????????if(next_word.length()==0)
????????{
????????????output.add(now);
????????}
????????else
????????{
????????????String?y?=?next_word.substring(0,1);//獲得當前數字
????????????String?x?=?phone.get(y);??//獲得每個數字代表的字母
????????????for(int?i=0;i<x.length();i++)
????????????{
????????????????String?z?=?x.substring(i,i+1);
????????????????back(now.concat(z),next_word.substring(1));
????????????}
????????}
????}
}
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Leetcode--17.电话号码的字母组合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NIO基础
- 下一篇: idea中每次push/pull都需要输