• Leetcode letter-combinations-of-a-phone-number(DFS)


    题目描述

    给出一个仅包含数字的字符串,给出所有可能的字母组合。
    数字到字母的映射方式如下:(就像电话上数字和字母的映射一样)
    Input:Digit string "23"Output:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
    注意:虽然上述答案是按字典序排列的,但你的答案可以按任意的顺序给出
     
    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.
     
    思路:使用深度遍历
    class Solution {
    public:
        vector<string> letterCombinations(string digits) {
            vector<string> dict{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            vector<string> res;
            int n = digits.size();
            string out;
            DFS(digits,0,dict,out,res);
            return res;
        }
        void DFS(string digits,int level, vector<string> dict,string &out, vector<string> &res)
        {
            if(level == digits.size())
                res.push_back(out);
            else
            {
                string s = dict[digits[level]-'0'];
                for(int i = 0;i<s.size();++i)
                {
                    out.push_back(s[i]);
                    DFS(digits,level+1,dict,out,res);
                    out.pop_back();
                }
            }
        }
    };
  • 相关阅读:
    i春秋CTF-web-upload
    [转]SEP 11.x 迁移
    通过SEP禁用USB
    通过SEP屏蔽共享文件夹
    SEP图示
    离线更新SEPM服务器的病毒定义库
    SEPM安装完之后的一些细节之处
    Bloomberg SEP 12.x 迁移小记
    LiveUpdate Adminstrator配置手册
    Symantec更新服务器
  • 原文地址:https://www.cnblogs.com/zl1991/p/12783467.html
Copyright © 2020-2023  润新知