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

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

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

#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()
{

}

  

时间: 2024-10-07 02:26:13

leetcode_17题——Letter Combinations of a Phone Number(简单题)的相关文章

leetcode第18题--Letter Combinations of a Phone Number

Problem: 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&

【leetcode刷题笔记】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", &q

刷题17. Letter Combinations of a Phone Number

一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" 二.我的做法 这个题目

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

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"Ou

Letter Combinations of a Phone Number leetcode java

题目: 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"

【Leetcode长征系列】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"

LeetCode Solution:Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 17652 Total Submissions: 66854My Submissions Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephon

【leetcode】Letter Combinations of a Phone Number

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" Outpu

LeetCode: Letter Combinations of a Phone Number [018]

[题目] 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"