LeetCode Letter Combinations of a Phone Number

题目:

Given a digit string, 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.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

题意:

给定一个电话的键盘,然后输入一个字符串,其中字符串上的数字表示的是电话键盘上对应的字母,那么僵所有对应的情况输出。

题解:

此题采用先将第一个数字对应的那些个字母保存进一个临时空间中,然后是第二个数字对应的那些字母,依次将第二个数字对应的字母遍历放到刚才第一个临时空间中。直接看代码。

public List<String> letterCombinations(String digits)
	{
	    List<String> list = new ArrayList<String>();
	    int length = digits.length();
	    if(length == 0 || digits == null)
	        return list;
		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.add("");    //一开始输入一个空
		for(int i = 0; i < digits.length(); i++)
		{
			List<String> temp = new ArrayList<String>();  //临时变量
			String chars = map[digits.charAt(i) - '0'];
			for(int j = 0; j < chars.length(); j++)
			{
				for(int k = 0; k < list.size(); k++)
				{
					temp.add(list.get(k) + chars.charAt(j));   //这里是关键
				}
			}
			list = temp;
		}
		return list;
 	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值