1st round, 17 Letter Combinations of a Phone Number

这道题,是给我启蒙dfs算法的一道题,印象很深刻!本质上就是一棵多叉树的path 遍历,是对二叉树的扩展。

二叉树是普通多叉树的特例,可以用left,right指针来标记左右子节点,但是多叉树这样的话就没办法,也没必要用多个指针。。。说到这,我突然想到了一道题目,可以用2分法,然后我又想到了3分法 [waiting for link] ,但是和这里一样,二叉和二分就是比较有代表性的,有更多的3456分其实意义和作用都并不大。。

好了不扯太多,写把代码黏贴过来


public class Solution {
    public List<String> letterCombinations(String digits) {
// 第二次 1.25.17 用dfs在来实现一次。。。
    List<String> list= new ArrayList<String>();
    if(digits==null || digits.equals("")) return list;
    List<char[]> map = new ArrayList<char[]>();
    map.add(new char[]{' '});
    map.add(new char[]{});
    map.add(new char[]{'a','b','c'});
    map.add(new char[]{'d','e','f'});
    map.add(new char[]{'g','h','i'});
    map.add(new char[]{'j','k','l'});    
    map.add(new char[]{'m','n','o'});
    map.add(new char[]{'p','q','r','s'});
    map.add(new char[]{'t','u','v'});
    map.add(new char[]{'w','x','y','z'});
    
    dfs(digits, 0, map, "", list);
    return list;
    }

    private void dfs(String digits, int index, List<char[]> map, String s, List<String> list){
        if(index==digits.length()){
            list.add(s);
            return;
        }
        
        for(int i=0; i<map.get(digits.charAt(index)-'0').length; i++){
            dfs(digits, index+1, map, s+map.get(digits.charAt(index)-'0')[i], list);    
        }
    


        // 1.24 看着这题很熟悉,也不是很难的样子,但是感觉实现起来很生疏。。没有立马想起解题的套路,和应对方式。。。
        // 不爽,于是怒着也要写出来。。。
//        List<String> list= new ArrayList<String>();
//        if(digits==null || digits.equals("")) return list;

/*        List<List<Character>>map= new ArrayList<List<Character>>(); 
        map.add(new ArrayList<Character>(Arrays.asList(' ')));
        map.add(new ArrayList<Character>(Arrays.asList(''))); //
        map.add(new ArrayList<Character>(Arrays.asList('a','b','c')));
        map.add(new ArrayList<Character>(Arrays.asList('d','e','f')));
        map.add(new ArrayList<Character>(Arrays.asList('g','h','i')));
        map.add(new ArrayList<Character>(Arrays.asList('j','k','l')));
        map.add(new ArrayList<Character>(Arrays.asList('m','n','o')));
        map.add(new ArrayList<Character>(Arrays.asList('p','q','r','s')));
        map.add(new ArrayList<Character>(Arrays.asList('t','u','v')));
        map.add(new ArrayList<Character>(Arrays.asList('w','x','y','z')));
        // 这个用List<Character>来存放Char,就是为了用 for(char ch: map[index]) 这个简单的遍历方式 
        // 但是!!!用数组的话,不能存放空字符,所以1对应的字符是不能通过这种方式实例化的。。。!!可是!!我又看到了怎么解决这个问题,就是做一个 list<Char[]>,对于空char[], 可以这么来初始化: list.add(new Char[]{}); // 就是什么都不写,嗯,有点意思哈, https://leetcode.com/problems/letter-combinations-of-a-phone-number/
        // 我在网上看了几种解法,也都是用String来存储。。。而不是字符数组。。所以就不用纠结了,考虑到String遍历起来也不是太麻烦其实用也可以的。毕竟只是实习面试。。。
*/

//  以下是第二次1.24.17,用循环来实现。。。因为本题是找到所有解。。。 我现在用dfs再实现一次
/*        String[] map = new String[10];
		map[0]=" ";
		map[1]="";
		map[2]="abc";
		map[3]="def";
		map[4]="ghi";
		map[5]="jkl";
		map[6]="mno";
		map[7]="pqrs";
		map[8]="tuv";
		map[9]="wxyz";  
		// 一定的数据结构,决定了遍历的方式。。。如果像现在这样在把char全部存放在String里面,那遍历这些String只能用str.charAt();

        list.add("");
        for(int i=0; i<digits.length(); i++){
            List<String> curr = new ArrayList<String>();
            String append = map[digits.charAt(i)-'0'];
            for(String str: list){
                for(int j=0; j<append.length(); j++){
                    curr.add(str+append.charAt(j));
                }
            }
            list=curr;
        }
        
        return list;*/
    

// 下面是大概1.10左右做的。。。利用的是dfs方法,这个方法我明显还是掌握得不深刻。。。打开dfs的tag,看到相关的题有generate paranthesis,似乎有了一点感觉dfs在代码上怎么设计。。。简单的说,就是利用递归调用,递归的化肯定有出口,出口怎么实现,就是讲要递进的变量作为 输入参数,有起始值。。。进入函数首先检查这个参数是不是到了exist的case。。。如果是那就要return了。。。如果不是,那就是做处理,同时调用本身函数,这个时候,递进的变量必须做适当改变。。。这个道理很类似 while loop的条件,和loop内部的参数变化!!
// 感觉是这么回事啊。。。明天可以好好总结一下!!


/*        if(digits.equals("") || digits==null) return new ArrayList<String>();

        String[] map = new String[10];
		map[0]=" ";
		map[1]="";
		map[2]="abc";
		map[3]="def";
		map[4]="ghi";
		map[5]="jkl";
		map[6]="mno";
		map[7]="pqrs";
		map[8]="tuv";
		map[9]="wxyz";        

        List<String> list = new ArrayList<String>();
        list.add("");
        List<String> result = new ArrayList<String>();
        return iterateMyTree(digits, 0, map, list, result);
    }
    
    public List<String> iterateMyTree(String digits, int level, String[] map, List<String> input, List<String> result){
        if(level==digits.length()) return input; // base case, exit case
        
        for(String str: input){  // go through every elem in input
            if(map[digits.charAt(level)-'0'].length()==0) result=input;  // check whether adding list has something, if empty
            else if(map[digits.charAt(level)-'0'].length()>0){   // if not empty
                for(int i=0; i<map[digits.charAt(level)-'0'].length(); i++){
                    result.add(str+map[digits.charAt(level)-'0'].charAt(i));
                }
            }
        }
        List<String> nextResult = new ArrayList<String>();
        return iterateMyTree(digits, level+1, map, result, nextResult);*/
    }
}





(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、付费专栏及课程。

余额充值