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", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

想了半天不知道怎么做,对递归真心不熟悉。特意看了算法导论的两个优先搜索,即广度优先和深度优先 bfs 和 dfs,这题用到的是dfs方法。

void dfs(int len, string s, string t, vector<string> &ans)
{
    string ss[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    if (len == s.size()) // 当t已经存够s长度个的时候就可以push到答案中并返回
    {
        ans.push_back(t);
        return;
    }
    for (int i = 0; i < ss[s[len] -‘0‘].size(); ++i) // 进行深度优先搜索,len相当于depth
    {
        dfs(len + 1, s, t + ss[s[len] - ‘0‘][i], ans);
    }
}
vector<string> letterCombinations(string digits)
{
    vector<string> ans;
    ans.clear();
    if (digits.size() == 0)
    {
        ans.push_back("");
        return ans;
    }
    dfs(0, digits, "", ans);// 从第零层temp str 为空开始往深处搜索
    return ans;
}
时间: 2024-12-25 00:02:21

leetcode第18题--Letter Combinations of a Phone Number的相关文章

【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 (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" Output: ["ad", "ae",

【Leetcode】17、Letter Combinations of a Phone Number

题目 Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letter

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&qu

LeetCode(17)Letter Combinations of a Phone Number

题目如下: Python代码: class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ if not digits: return [] dic = {'2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv',

【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

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][JavaScript]Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 40709 Total Submissions: 157759My Submissions Question Solution Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just l

【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