欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

电话号码的数字组合

程序员文章站 2024-01-24 17:34:16
...

电话号码的数字组合 

string a[] = {
	{"  "},
	{"  "},
	{"abc"},
	{"def"},
	{"ghi"},
	{"jkl"},
    {"mon"},
	{"pqrs"},
	{"tuv"},
	{"wxyz"}
};
vector<string> res;

class Solution {
public:
	vector<string> letterCombinations(string digits) {
		res.clear();
		if (digits.size() == 0)
			return res;
		else
		{
			string s = "";
			dfs(digits, 0, s);
			return res;
		}			
	}
	void dfs(string digits, int index, string s)
	{
		if (index >= digits.size())
		{
			res.push_back(s);
			return;
		}
		for (int i = index; i < digits.size(); i++)
		{
			int m = digits[i] - '0';
			for (int j = 0; j < a[m].size(); j++)
			{
				s += a[m][j];
				dfs(digits, index+1,s);
				s.replace(s.size() - 1, 1, "");
			}

		}
	}
};

 

相关标签: leetcode