#17 Letter Combinations of a Phone Number

Description

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
在这里插入图片描述

Example

Input: “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]

解题思路1


一如既往
用最傻的办法
就是枚举
先把所有的键盘列出来
之后运算的过程和人手动运算过程一样
先把第一个数字对应的字符列出来
然后对每一个字符根据下一个数字再扩展成n个不同的字符串
放在代码里的话
i i i 就是数字数量(一共有多少个数字)
j j j 就是这个数字对应了几种字符
k k k 就是先前的答案库中已经有了多少种组合
假设之前已经有了3种组合,而当前数字又对应了3种字符,那么经过一次循环之后就会有9中组合,然后再对应下个数字进行新的排列组合

class Solution {
    public List<String> letterCombinations(String digits) {
        List<List<String>> phone = new ArrayList<>();
        List<String> temp1 = new ArrayList<>();
        temp1.add("a");
        temp1.add("b");
        temp1.add("c");
        phone.add(temp1);
        
        List<String> temp2 = new ArrayList<>();
        temp2.add("d");
        temp2.add("e");
        temp2.add("f");
        phone.add(temp2);
        
        List<String> temp3 = new ArrayList<>();
        temp3.add("g");
        temp3.add("h");
        temp3.add("i");
        phone.add(temp3);
        
        List<String> temp4 = new ArrayList<>();
        temp4.add("j");
        temp4.add("k");
        temp4.add("l");
        phone.add(temp4);
        
        List<String> temp5 = new ArrayList<>();
        temp5.add("m");
        temp5.add("n");
        temp5.add("o");
        phone.add(temp5);
        
        List<String> temp6 = new ArrayList<>();
        temp6.add("p");
        temp6.add("q");
        temp6.add("r");
        temp6.add("s");
        phone.add(temp6);
        
        List<String> temp7 = new ArrayList<>();
        temp7.add("t");
        temp7.add("u");
        temp7.add("v");
        phone.add(temp7);
        
        List<String> temp9 = new ArrayList<>();
        temp9.add("w");
        temp9.add("x");
        temp9.add("y");
        temp9.add("z");
        phone.add(temp9);
        
        int i, j, k;
        String t = "";
        int[] nums = {3, 3, 3, 3, 3, 4, 3, 4};
        int total_num = 1;
        List<String> answer = new ArrayList<>();
        List<String> temp_answer = new ArrayList<>();
        if(digits.length() == 0)
            return answer;
        for(i = 0; i < nums[digits.charAt(0) - '2']; i++){
            answer.addAll(phone.get(digits.charAt(0) - '2'));
        }
        for(i = 1; i < digits.length(); i++){
           temp_answer.clear();
            for(j = 0; j < nums[digits.charAt(i) - '2']; j++){
                for(k = 0; k < answer.size(); k++){
                    temp_answer.add(answer.get(k) + phone.get(digits.charAt(i) - '2').get(j));
                }
            }
            answer.clear();
            answer.addAll(temp_answer);
        }
        List<String> new_answer = new ArrayList<>(new HashSet<>(answer));
        return new_answer;
    }
}

其他

  • 在一开始的phone添加temp的时候……添加的只是指针,所以不能用一个temp然后clear完继续填充下一个,而需要8个不同的temp对phone的list进行填充
  • 当对一个list用别的list进行add操作的时候,用的是addAll(another_list)
  • 摸了一下discussion,快的人都用递归QuQ

解题思路2

时隔2年,又开始刷了
这次摸了下递归
就是个深搜dfs,每次轮流往下走,走到最后输出给全局变量最后返回即可

class Solution {
    private Map<Character, String> mappedNumbers = Map.of ('2', "abc", '3', "def",'4',"ghi",'5',"jkl",'6',"mno",'7',"pqrs",'8', "tuv", '9', "wxyz");
    private String number;
    private List<String> finalAnswer;
    public List<String> letterCombinations(String digits) {
        if(digits.length() == 0)
            return new ArrayList<>();
        
        number = digits;
        finalAnswer = new ArrayList<>();
        
        dfs(0, "");
        return finalAnswer;
    }
    
    public void dfs(int index, String answer){
        if(index == number.length()){
            finalAnswer.add(answer);
            return;
        }
        
        String currentStr = mappedNumbers.get(number.charAt(index));
        for(int i = 0; i < currentStr.length(); i++){
            dfs(index + 1, answer + currentStr.charAt(i));
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(Telephone Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table: Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indi- cated in the above table to develop the seven-letter word “NUMBERS.” Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls. Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e., “TAKEOUT”). Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtaposi- tions of letters. It’s possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be happy to know that the number corresponds to “PETCARE.” Write a program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words. Avoid phone numbers with the digits 0 and 1.
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值