zoukankan      html  css  js  c++  java
  • 【medium】17. Letter Combinations of a Phone Number 数字组合

    class Solution {
    public:
        vector<string> ans; // answers
        
        // dfs实现
        void dfs (int x, int l, string str, string digits, char phone[][4]){
            if (x==l){
                ans.push_back(str);
                return;
            }
            int num = digits[x] - '0';
            for (int i=0;i<4;i++){
                if (phone[num][i]){
                    dfs(x+1, l, str+phone[num][i], digits, phone);
                }
            }
        }
        
        vector<string> letterCombinations(string digits) {
            char phone[10][4] = {{" "}, {}, {'a', 'b', 'c'}, {'d', 'e', 'f'}, 
                                 {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'},
                                 {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, 
                                 {'w', 'x', 'y', 'z'}};
            
            if (digits.length() == 0)
                return ans;
            
            // dfs算法!
            dfs(0, digits.length(), "", digits, phone);
                
            return ans;
            
        }
    };

    给一个不包含'0''1'的数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合。

    下图的手机按键图,就表示了每个数字可以代表的字母。

    12
    ABC
    3
    DEF
    4
    GHI
    5
    JKL
    6
    MNO
    7
    PQRS
    8
    TUV
    9
    WXYZ
  • 相关阅读:
    实现系统托盘
    MDI窗体应用
    C#窗体的常用设置
    什么是UWP应用
    关于用js写缓动 动画
    关于tab栏切换的解析
    函数
    for循环
    if语句
    js
  • 原文地址:https://www.cnblogs.com/sherry-yang/p/10799005.html
Copyright © 2011-2022 走看看