首页 > 其他 > 详细

LeetCode——Letter Combinations of a Phone Number

时间:2014-07-21 16:44:12      阅读:340      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣

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.

原题链接:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/

题目:给定一个数字字符串,返回所有数字能代表的字母的组合。

思路:将数字和对应的字母放到一个哈希表中。

	static final HashMap<Character, String> map = new HashMap<Character, String>() {
		{
			put('2', "abc");
			put('3', "def");
			put('4', "ghi");
			put('5', "jkl");
			put('6', "mno");
			put('7', "pqrs");
			put('8', "tuv");
			put('9', "wxyz");
		}
	};

	public static List<String> letterCombinations(String digits) {
		List<String> res = new ArrayList<String>();
		res.add("");

		for (int i = 0; i < digits.length(); i++) {
			List<String> tmp = new ArrayList<String>();
			for (String str : res) {
				String letters = map.get(digits.charAt(i));
				for (int j = 0; j < letters.length(); j++)
					tmp.add(str + letters.charAt(j));
			}
			res = tmp;
		}
		return res;
	}



LeetCode——Letter Combinations of a Phone Number

原文:http://blog.csdn.net/laozhaokun/article/details/38019017

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!