• 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", "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.

    题目解答:

    题目要求根据输入的数字串,判断出可能产生的字母序列。首先,需要存储每个数字与字母字符的映射关系,在这里,我们使用map结构来存储。之后,不断使用迭代实现对可能产生的字母序列的遍历过程。

    代码:

    class Solution {
    public:
        Solution()  //建立映射关系
        {
            vector<char> _0_;
            digitMapChars['0'] = _0_;
            
            vector<char> _1_;
            digitMapChars['1'] = _1_;
            
            vector<char> _2_;
            _2_.push_back('a'); 
            _2_.push_back('b');
            _2_.push_back('c');
            digitMapChars['2'] = _2_;
            
            vector<char> _3_;
            _3_.push_back('d');
            _3_.push_back('e');
            _3_.push_back('f');
            digitMapChars['3'] = _3_;
            
            vector<char> _4_;
            _4_.push_back('g');
            _4_.push_back('h');
            _4_.push_back('i');
            digitMapChars['4'] = _4_;
            
            vector<char> _5_;
            _5_.push_back('j');
            _5_.push_back('k');
            _5_.push_back('l');
            digitMapChars['5'] = _5_;
            
            vector<char> _6_;
            _6_.push_back('m');
            _6_.push_back('n');
            _6_.push_back('o');
            digitMapChars['6'] = _6_;
            
            vector<char> _7_;
            _7_.push_back('p');
            _7_.push_back('q');
            _7_.push_back('r');
            _7_.push_back('s');
            digitMapChars['7'] = _7_;
            
            vector<char> _8_;
            _8_.push_back('t');
            _8_.push_back('u');
            _8_.push_back('v');
            digitMapChars['8'] = _8_;
            
            vector<char> _9_;
            _9_.push_back('w');
            _9_.push_back('x');
            _9_.push_back('y');
            _9_.push_back('z');
            digitMapChars['9'] = _9_;
        }
        
        vector<string> letterCombinations(string digits) {
        	vector<string> fres; //最终结果存储在fres中
            if(digits == "")
                return fres;
            string::iterator sit = digits.begin();  //使用sit迭代器来完成对digits这个字符串的访问
            list<string> res;  //结果存储在res中
            
            int former_size = 1;
            while(sit != digits.end())
            {
                if(digitMapChars.find(*sit) == digitMapChars.end())  //出现未知字符
                {
                    break;
                }
                int tmp_size = digitMapChars[*sit].size();
                if(res.empty())
                {
                    former_size *= digitMapChars[*sit].size();
                    for(vector<char>::iterator vit = digitMapChars[*sit].begin(); vit != digitMapChars[*sit].end(); vit++)
                    {
                    	string t = "";
                        res.push_back(t + *vit);
                    }
                }
                else
                {
                    int former_num = former_size;
                    former_size *= digitMapChars[*sit].size();
                    int cur_num = digitMapChars[*sit].size();
                    while(former_num != 0)
                    {
                        string strtmp = res.front();
                        for(int i = 0;i < cur_num;i++)
                        {
                            res.push_back(strtmp + digitMapChars[*sit][i]);
                        }
                        res.pop_front();
                        former_num--;
                    }
                }
                sit++;  //使用sit来实现对字符串中数字的遍历
            }
            
            for(list<string>::iterator sit = res.begin(); sit != res.end(); sit++)
            {
            	//cout << *sit << endl;
                fres.push_back(*sit);
            }
            return fres;
            
        }
    private:
        map<char,vector<char> > digitMapChars;
    };
    

      

  • 相关阅读:
    Android Shell命令dumpsys
    查看Android内存的8中方法
    Viewpager 的相关总结
    FileZilla等软件搭建ftp服务器
    Linux系统下邮件服务器的搭建(Postfix+Dovecot)
    PHP 打乱数组
    PHP 日期 加减 月数,天数,周数,小时,分,秒等等
    PHP 常用函数
    111
    phpStudy启动失败提示:缺少VC9运行库
  • 原文地址:https://www.cnblogs.com/CodingGirl121/p/5584408.html
Copyright © 2020-2023  润新知