给定一个数字字符串,输出对应的所有组合

 

#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
map<int, string> numtostr{
	{2,"abc"},
    {3,"def"},
    {4,"hij"},
    {5,"klm"},
    {6,"nop"},
    {7,"qrs"},
    {8,"tuv"},
    {9,"wzx"} };
vector <string>res;
class solution
{
public:
	vector<string> test(string digital)
	{
		string tmp = "";
		if (digital.length() == 0)
		{
			res.push_back(tmp);
			return res;
		}
		dfs(digital, tmp, 0);
		return res;
	}
private:
	void dfs(string digital, string tmp, int index)
	{
		if (index == digital.length())
		{
			res.push_back(tmp);
			return;
		}
		int k = digital[index] - '0';
		for (int i = 0; i < numtostr[k].length(); i++)
			dfs(digital,tmp+numtostr[k][i],index+1);
	}
};
int main()
{
	string tmp = "234";
	class solution a;
	vector<string> res=a.test(tmp);
	for (int i = 0; i < res.size(); i++)
		cout << res[i] << endl;
	
	system("pause");
	return 0;
}
发布了98 篇原创文章 · 获赞 1 · 访问量 4495

猜你喜欢

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