zoukankan      html  css  js  c++  java
  • 【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;                
        }
    };
  • 相关阅读:
    iOS中网络请求判断是否设置代理
    swif开发笔记12-Animations
    swift开发笔记11
    swift开发笔记06
    Idea热部署jrebel失败
    Oracle连接知识
    Idea安装及其简介
    博客园cnblog发布word
    en笔记音标
    测试案例小问题
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6786345.html
Copyright © 2011-2022 走看看