码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-04 09:32:52      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:leetcode   acm   算法   letter combinations   

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

标签:leetcode   acm   算法   letter combinations   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!