题解 | #电话号码的字母组合#

电话号码的字母组合

https://www.nowcoder.com/practice/2d3a1e71112546ac836700ccbd1f5936

import java.util.*;

/**
 * NC329 电话号码的字母组合
 * @author d3y1
 */
public class Solution {
    private HashMap<Character, String> numLettersMap = new HashMap<Character, 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");
    }};

    private int LEN;
    private ArrayList<String> result = new ArrayList<>();

    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 程序入口
     *
     * @param num string字符串
     * @return string字符串ArrayList
     */
    public ArrayList<String> phoneNumber (String num) {
        LEN = num.length();

        dfs(num, 0, "");

        return result;
    }

    /**
     * dfs
     * @param num
     * @param depth
     * @param word
     */
    private void dfs(String num, int depth, String word){
        if(depth == LEN){
            result.add(word.toLowerCase());
            return;
        }

        for(char ch: numLettersMap.get(num.charAt(depth)).toCharArray()){
            dfs(num, depth+1, word+ch);
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:21
被夸真的超级开心,好可爱的姐姐
码农索隆:老色批们不用脑补了,我把金智妮的图找来了查看图片
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 11:15
点赞 评论 收藏
分享
MinJerous:虽然我一直说 计算机不怎么卡学历 但是至少得一本
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务