LeetCode *** 17. Letter Combinations of a Phone Number

来源:互联网 发布:java在线答题系统源码 编辑:程序博客网 时间:2024/05/20 00:50

题目:

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.


分析:

“你是一封信我是邮差,忙着去护送来不及拆开,里面完美的世界~”。。


代码:

class Solution {public:    vector<string> letterCombinations(string digits) {        if(digits.length()<1)return vector<string>();                string d[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};                vector<string> res({""});                for(int i=0;i<digits.size();++i){                        vector<string> tmp;            for(int j=0;j<d[digits[i]-'0'].length();++j)                for(int k=0;k<res.size();++k)                    tmp.push_back(res[k]+d[digits[i]-'0'][j]);            res=tmp;        }        return res;            }};

0 0
原创粉丝点击