Letter Combinations of a Phone Number

原题链接:

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


思路:

输入一串0-9的数字,每个数字对应着电话上的1个或多个字符,要求出所有可能的字符组合,以List形式返回。

假设输入“23”,先取第一个字符2,对应的字符串是“abc”,那么此时的List内元素是“a”,"b","c"。再取第二个字符3,对应的字符串是“def”,那么此时List中的元素是“ad”,"ae","af","bd","be","bf","cd","ce","cf"。可以发现,输入的号码字符串里每多一个数字字符,就会取出原List中所有的元素,给每一个元素字符串加新的字符构成新的字符串然后将这个字符串加到List中,而原List中所有的元素都会消失。原List中的元素就像是一个个的根节点,输入的数字字符每多一个,每个根节点就会生成很多子节点,当子节点生成之后,就会把根节点全部去除,从而新生成的子节点变成了新的根节点可以开始下一轮循环,直到输入字符串的最后一个元素。我们可以利用队列的FIFO特性来描述这样一种关系,先入队的根节点先出队来构造子节点入队。最原始的根节点,使用空串“”。注意这样一种情况,在生成子节点的过程中会出现根节点和子节点同时在队中的情况,比如队中有“a”,"b","c",现在开始生成子节点,那么从队列中取出“a”,生成“ad”,“ae”,"af",这时"b","c",“ad”,"ae","af"同时在队中,那么只可以有“b”,"c"来作为根节点,而“ad”,"ae","af"不可以作为根节点。还有一种情况,为了方便操作,我们使用了“”作为最原始的根节点,如果输入字符串长度不为0,自然这个“”会被remove。但是如果输入字符串为长度为0,也就是根本就不会进行生成子节点的操作,“”不会被remove,所以要在开始时加一个判断,如果输入字符串长度为0,则直接返回一个空的List。


LinkedList提供了方法以支持队列的行为,并且实现了Queue接口。

offer() 将元素插到队尾或者返回false。

peek()不移除的情况下返回队头,没有队头返回null。

element()不移除的情况下返回队头,没有队头抛出异常。

poll()移除的情况下返回队头,没有队头返回null。

remove()移除的情况下返回队友,没有队头抛出异常。


iint Character.getNumericValur(char c); char到int的转换。



    public List<String> letterCombinations(String digits) {
        LinkedList<String> queue = new LinkedList<String>();
        if (digits.length()==0){
        	return queue;
        }
        String[] map = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        queue.add("");
        for(int i=0; i<digits.length(); i++) {
        	int key = Character.getNumericValue(digits.charAt(i));
        	while(queue.peek().length() == i) {
        		String t = queue.remove();
        		for(char s: map[key].toCharArray()) {
        			queue.offer(t+s);
        		}
        	}
        }
        return queue;
    }




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

余额充值