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

leetcode_17题——Letter Combinations of a Phone Number(简单题)

时间:2015-05-19 20:51:44      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

这道题要求手机上,不同的数字所对应的字母的组合,就是一步步往上求就可以了,可能有点类似于动态规划的

先求解子问题,再求出总的

#include<iostream>
#include<vector>
#include <map>
#include<string>
using namespace std;

static string str[10]={"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};


vector<string> To_next(vector<string>& str1,char a)
{
	vector<string> lastresult;
	string b=str[a-48];
	int len_b=b.size();
	int len_str1=str1.size();

	string temp;
	for(int i=0;i<len_b;i++)
	{
		for(int j=0;j<len_str1;j++)
			lastresult.push_back(str1[j]+b[i]);
	}
	return lastresult;
}


vector<string> letterCombinations(string digits) {
	vector<string> str0;
	int len=digits.size();
	if(digits.empty())
		return str0;
	for(int i=0;i<str[digits[0]-48].size();i++)
	{
		string temp1;
		temp1.push_back(str[digits[0]-48][i]);
		str0.push_back(temp1);
	}

	for(int i=1;i<digits.size();i++)
	{
		vector<string> str1=To_next(str0,digits[i]);
		str0=str1;
	}
	return str0;
}

int main()
{

}

  

leetcode_17题——Letter Combinations of a Phone Number(简单题)

标签:

原文地址:http://www.cnblogs.com/yanliang12138/p/4515368.html

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