Leetcode 17 Letter Combinations of a Phone Number

原创 2016年08月29日 19:44:28

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.

题意为输出所有按键可能表示的字母组合。

因为不知道按键组合有多长,所以可以用搜索来做,

为了效率我用的是BFS,不过好像也没快到哪里去。

先打印出字典表,用队列进行搜索,算法的复杂度最优就行了,不必在意常数项的问题。

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        string dic[11]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        vector<string> result;
        queue <string> q;
        q.push("");
        for(int i=0;i<digits.length();i++)
        {
            int cnt=q.size();
            while(cnt--)
            {
                string temp=q.front();
                q.pop();
                for(int j=0;j<dic[digits[i]-'0'].length();j++)
                    q.push(temp+dic[digits[i]-'0'][j]);
            }
        }
        if(q.front()=="") q.pop();
        while(!q.empty())
        {
            result.push_back(q.front());
            q.pop();
        }
        return result;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A ...

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A ...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A map...

17. Letter Combinations of a Phone Number

 题目:Letter Combinations of a Phone Number Given a digit string, return all possible letter combi...

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent.A mapp...

#17 Letter Combinations of a Phone Number

题目链接:https://leetcode.com/problems/letter-combinations-of-a-phone-number/ Given a digit string,...

17. Letter Combinations of a Phone Number

一、题目:Given a digit string, return all possible letter combinations that the number could represent.A...

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A ma...

17. Letter Combinations of a Phone Number

题目:Letter Combinations of a Phone Number原题链接:https://leetcode.com/problems/letter-combinations-of-a-...

17. Letter Combinations of a Phone Number

此道题目为经典的DFS题目,可以用DFS解决。很长时间没有写递归题目了,所以我的解法占用空间较大,比较高效的写法是只在栈中维护一个字符序列,而我的解法则是维护了3^N个字符序列,其中N为输入的digi...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)