首页 > 代码库 > leetcode.17-----------Letter Combinations of a Phone Number

leetcode.17-----------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"].

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