[LeetCode] Letter Combinations of a Phone Number(bfs)

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.

用queue实现bfs:

class Solution {
public:
    map<char,string> m;
    vector<string> letterCombinations(string digits) {
        vector<string> result;
        m[‘2‘]="abc";
        m[‘3‘]="def";
        m[‘4‘]="ghi";
        m[‘5‘]="jkl";
        m[‘6‘]="mno";
        m[‘7‘]="pqrs";
        m[‘8‘]="tuv";
        m[‘9‘]="wxyz";
        queue<string> q;
        string s;
        q.push(s);
        result = bfs(q,digits);
        return result;
    }
    vector<string>  bfs(queue<string> q,string &digits){
        vector<string> result;

        int len = digits.size();
        if(len==0){
            string s;
            result.push_back(s);
            return result;
        }

        while(!q.empty()){
            string s = q.front();
            int n = s.size();
            q.pop();
            if(n==len){
                result.push_back(s);
                continue;
            }

            string s0(s);
            char c = digits[n];
            int alphaNum = m[c].size();
            for(int i =0;i<alphaNum;i++){
                s.push_back(m[c][i]);
                q.push(s);
                s = s0;
            }
        }

    return result;
    }
};
时间: 2024-11-12 18:04:37

[LeetCode] Letter Combinations of a Phone Number(bfs)的相关文章

LeetCode: Letter Combinations of a Phone Number [018]

[题目] 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"

Leetcode:Letter Combinations of a Phone Number 手机键盘字母映射

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" Outp

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

LeetCode: Letter Combinations of a Phone Number 解题报告

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

LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)

翻译 给定一个数字字符串,返回所有这些数字可以表示的字母组合. 一个数字到字母的映射(就像电话按钮)如下图所示. 输入:数字字符串"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 备注:尽管以上答案是无序的,如果你想的话你的答案可以是

LeetCode——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", &q

[LeetCode] 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", &q

[LeetCode] 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", &q

24.Letter Combinations of a Phone Number(电话号对应的字符组合)

Level: ??Medium 题目描述: Given a string containing digits from 2-9 inclusive, 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. Note that 1 does no