zoukankan      html  css  js  c++  java
  • 17. Letter Combinations of a Phone Number

    import java.util.ArrayList;
    import java.util.List;
    
    public class Solution {
        public List<String> letterCombinations(String digits) {
            int size=digits.length();
            List<String> res=new ArrayList<String>();
            if(size==0)
                return res;
            String[] map = new String[] {  
                " ",  
                "1", "abc", "def",  
                "ghi", "jkl", "mno",  
                "pqrs", "tuv", "wxyz"  
             }; 
            int index=0;
            StringBuilder temp=new StringBuilder();
            bfs(digits,size,index,res,temp,map);
            return res;
            
        }
        
        public void bfs(String digits,int size,int index,List<String> res,StringBuilder temp,String[] map)
        {
            
            if(size==index)
            {
                //表示已经完成了一个字符串的拼接了
                res.add(temp.toString());
                return ;
            }
            
            char c=digits.charAt(index);
            for(int j=0;j<map[c-'0'].length();j++)
            {
                index++;
                temp.append(map[c-'0'].charAt(j));
                bfs(digits,size,index,res,temp,map);
                temp.deleteCharAt(temp.length()-1);
                index--;
            }
        }
    }
  • 相关阅读:
    SpringMVC-Day1 (课题有用)
    Spring-Day3
    Spring-Day2
    fix mac
    新西兰产假(陪产假)
    nz 国内航空
    英文句子
    centos7.2安装swoole扩展
    linux搭建vue框架部署环境
    微信扫描带参数二维码事件
  • 原文地址:https://www.cnblogs.com/aguai1992/p/5692116.html
Copyright © 2011-2022 走看看