LeetCode刷题经验总结记录--17. 电话号码的字母组合

https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例 :

输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]

解法:

package leetcode2;

import java.util.*;

/**
 * 2021/3/9 15:54
 *
 * @Author ayue
 */
public class Solution17 {
    public List<String> letterCombinations(String digits) {//队列解法,前出后进
        List<String> strings = new LinkedList<String>();
        Map<Character, String[]> map = new HashMap<Character, String[]>() {{    //存储为数组更好操作
            put('2', new String[]{"a", "b", "c"});
            put('3', new String[]{"d", "e", "f"});
            put('4', new String[]{"g", "h", "i"});
            put('5', new String[]{"j", "k", "l"});
            put('6', new String[]{"m", "n", "o"});
            put('7', new String[]{"p", "q", "r", "s"});
            put('8', new String[]{"t", "u", "v"});
            put('9', new String[]{"w", "x", "y", "z"});
        }};
        Queue<String> queue = new LinkedList<String>();
        char[] chars = digits.toCharArray();
        for (char c : chars) {
            spli(queue, map.get(c));
        }
        for (String q : queue) {
            strings.add(q);
        }
        
        return strings;
    }
    
    private void spli(Queue<String> queue, String[] strings) {
        if (queue.size() == 0) {
            for (String s : strings) {
                queue.add(s);
            }
        } else {
            int queueLen = queue.size();
            for (int i = 0; i < queueLen; ++i) {
                String str = queue.poll();
                for (String s : strings) {
                    queue.add(str + s);
                }
            }
        }
    }
    
    public List<String> letterCombinations2(String digits) {//回溯。遍历
        List<String> strings = new LinkedList<String>();
        if (digits.length() == 0) {
            return strings;
        }
        Map<Character, String[]> map = new HashMap<Character, String[]>() {{    //存储为数组更好操作
            put('2', new String[]{"a", "b", "c"});
            put('3', new String[]{"d", "e", "f"});
            put('4', new String[]{"g", "h", "i"});
            put('5', new String[]{"j", "k", "l"});
            put('6', new String[]{"m", "n", "o"});
            put('7', new String[]{"p", "q", "r", "s"});
            put('8', new String[]{"t", "u", "v"});
            put('9', new String[]{"w", "x", "y", "z"});
        }};
        flashBack(0, digits, new StringBuilder(), strings, map);
        return strings;
        
    }
    
    private void flashBack(int index, String digits, StringBuilder prefix, List<String> stringList, Map<Character, String[]> map) {
        if (index == digits.length()) {
            stringList.add(prefix.toString());
        } else {
            String[] strings = map.get(digits.charAt(index));
            for (int i = 0; i < strings.length; ++i) {
                prefix = prefix.append(strings[i]);//1
                flashBack(index + 1, digits, prefix, stringList, map);
                prefix = prefix.deleteCharAt(index);//将上上行添加的字符移除进行下一次循环
            }
        }
    }
    
    
    public static void main(String[] args) {
        Solution17 solution17 = new Solution17();
        String digits = "23";
        System.out.println(solution17.letterCombinations2(digits));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值