三天一题-23- 通过数字串读取对应的字母组合

题目地址:letteCombinationsPhoneNumber

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class LetterCombinations {
    public static void main(String[] args) {
        String str = "23456";
        long l = System.nanoTime();
       /*  StringBuffer stringBuffer = letterCombinations(str);
         System.out.println(stringBuffer);*/
        List<String> strings = letterCombinationsTwo(str);
        System.out.println(strings);
        System.out.println(System.nanoTime()-l);
    }

    public static StringBuffer letterCombinations(String str) {
        HashMap<Character, String[]> map = new HashMap<Character, String[]>();
        map.put('2', new String[]{"a", "b", "c"});
        map.put('3', new String[]{"d", "e", "f"});
        map.put('4', new String[]{"g", "h", "i"});
        map.put('5', new String[]{"j", "k", "l"});
        map.put('6', new String[]{"m", "n", "o"});
        map.put('7', new String[]{"p", "q", "r", "s"});
        map.put('8', new String[]{"t", "u", "v"});
        map.put('9', new String[]{"w", "x", "y", "z"});
        int length = str.length();
        int totalLen = 0;
        List<String[]> list = new ArrayList<String[]>();
        for (int i = 0; i < length; i++) {
            char c = str.charAt(i);
            String[] letterArr = map.get(c);
            list.add(letterArr);
            int letterLen = letterArr.length;
            totalLen = totalLen == 0 ? letterLen : totalLen * letterLen;
        }
        int size = list.size();
        String[] lastArr = list.get(size - 1);
        int lastLen = lastArr.length;
        StringBuffer res = new StringBuffer();
        for (int j = 0, k = 0; j < totalLen; j++, k++) {
            int i = 0;
            int preLen = totalLen;
            while (i < size - 1) {
                String[] strings = list.get(i);
                int curLen = strings.length;
                int tempLen = curLen;
                if (preLen != 0) {
                    tempLen = preLen/tempLen;
                }
                int val = j/ tempLen;
                while (val >= curLen) {
                    val = val -curLen;
                }
                String  partRes = strings[val == 0 ? 0 : val ];
                res.append(partRes);
                preLen = tempLen;
                i++;
            }
            res.append(lastArr[k]);
            if (totalLen != 0) {
                res.append(",");
            }
            //最外层置零
            if (k == lastLen - 1) {
                k = -1;
            }
        }
        return res;
    }

    //BFS(广度优先搜索)此方法借鉴(https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8064/My-java-solution-with-FIFO-queue)
    public static List<String> letterCombinationsTwo(String digits) {
        LinkedList<String> ans = new LinkedList<String>();
        if(digits.isEmpty()) return ans;
        String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        ans.add("");
        while(ans.peek().length()!=digits.length()){
            String remove = ans.remove();
            String map = mapping[digits.charAt(remove.length())-'0'];
            for(char c: map.toCharArray()){
                ans.addLast(remove+c);
            }
        }
        return ans;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值