首页 > 其他 > 详细

leetcode.17-----------Letter Combinations of a Phone Number

时间:2015-02-04 09:31:44      阅读:300      评论: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.

技术分享

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

class Solution {
public:
	const vector<string> keyboard{ " ", "", "abc", "def", // '0','1','2',...
		"ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
	vector<string> letterCombinations(const string &digits) {
		vector<string> result;
		dfs(digits, 0, "", result);
		return result;
	}
	void dfs(const string &digits, size_t cur, string path,
		vector<string> &result) {
		if (cur == digits.size()) {
			result.push_back(path);
			return;
		}
		for (auto c : keyboard[digits[cur] - '0']) {
			dfs(digits, cur + 1, path + c, result);
		}
	}
};







leetcode.17-----------Letter Combinations of a Phone Number

原文:http://blog.csdn.net/chenxun_2010/article/details/43458247

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