Leetcode|Leetcode 17 电话号码的字母组合

【Leetcode|Leetcode 17 电话号码的字母组合】给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

Leetcode|Leetcode 17 电话号码的字母组合
文章图片
手机键盘示例
示例:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
思路:广度优先搜索。细节的话看代码,需要注意的点在于第一次搜索的时候ans里面是空的,所以需要特殊对待,先写入一次。
class Solution { public List letterCombinations(String digits) { //结果集 List ans = new ArrayList(); if(digits.length()<1){ return ans; } String[] str = new String[digits.length()]; //digits循环 for(int i = 0; i0){ for(String s : ans){ for(int i = 0; i

    推荐阅读