• 【LeetCode】017. 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", "af", "bd", "be", "bf", "cd", "ce", "cf"].
    

      

    题解:

    Solution 1 ()

    class Solution {
    public: 
        void dfs(string digits, int level, vector<string>& vv, string& s, vector<string> dict){
            if(level >= digits.size()) {
                vv.push_back(s);
                return;
            }
            string tmp = dict[digits[level] - '2'];
            for(int i=0; i<tmp.size(); ++i) {
                s.push_back(tmp[i]);
                dfs(digits, level+1, vv, s, dict);
                s.pop_back();
            }
        }
        vector<string> letterCombinations(string digits) {
            if(digits.empty()) return vector<string>();
            vector<string> dict = {"abc","def","ghi","jkl",
                                    "mno","pqrs","tuv","wxyz"};
            vector<string> vv;
            string s;
            dfs(digits, 0, vv, s, dict);
            return vv;                
        }
    };
  • 相关阅读:
    数据结构入门
    C语言入门-全局变量
    C语言入门-类型定义
    C++ 名称空间嵌套
    C++ 名称空间
    C++ 一些术语
    C++ new初始化与定位new运算符
    网络时间自动同步工具
    C++ 语言链接性
    C++ 函数和链接性
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6786345.html
Copyright © 2020-2023  润新知