电话号码的数字组合

 

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, "");
			}

		}
	}
};
发布了98 篇原创文章 · 获赞 1 · 访问量 4477

猜你喜欢

转载自blog.csdn.net/weixin_40823740/article/details/103707107