LeetCode 17-Letter Combinations of a Phone Number(DFS)

版权声明:转载请注明出处 https://blog.csdn.net/FGY_u/article/details/84325696

题目: https://leetcode.com/problems/letter-combinations-of-a-phone-number/

概述: 给一个手机键盘,按两个按钮,看有多少种字母组合的可能。

思路: 简单的DFS即可。

class Solution {
public:
    
    vector<string> res;
    
    void dfs(int n, string temp, string digits, string s[])
    {
        if(n == digits.length())
        {
            res.push_back(temp);
            return;
        }
        
        int number = digits[n] - '0';
        string chars = s[number];
        for(int i = 0; i < chars.length(); i++)
        {
            temp += chars[i];
            dfs(n + 1, temp, digits, s);
            temp = temp.substr(0, temp.size() - 1);
        }
        
        
    }
    
    vector<string> letterCombinations(string digits) {
        string s[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        int len = digits.size();
        if(len == 0)
            return res;
        dfs(0, "", digits, s);
        return res;
        
    }
};

猜你喜欢

转载自blog.csdn.net/FGY_u/article/details/84325696
今日推荐