#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”]

解题思路


一如既往
用最傻的办法
就是枚举
先把所有的键盘列出来
之后运算的过程和人手动运算过程一样
先把第一个数字对应的字符列出来
然后对每一个字符根据下一个数字再扩展成n个不同的字符串
放在代码里的话
i i 就是数字数量(一共有多少个数字)
j j 就是这个数字对应了几种字符
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

猜你喜欢

转载自blog.csdn.net/YY_Tina/article/details/86619260