美文网首页
17. Letter Combinations of a Pho

17. Letter Combinations of a Pho

作者: gpfworld | 来源:发表于2018-11-30 15:46 被阅读0次

    题目描述:

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

    mycode(c++):

    class Solution {
    public:
        vector<string> letterCombinations(string digits) {
            if(digits==""){
                vector<string> rt1;
                return rt1;
            }
            map<char,string> mp ;
            mp['2'] = "abc";
            mp['3'] = "def";
            mp['4'] = "ghi";
            mp['5'] = "jkl";
            mp['6'] = "mno";
            mp['7'] = "pqrs";
            mp['8'] = "tuv";
            mp['9'] = "wxyz";
            int len = 1;
            for( int i = 0;i< digits.length();i++) {
                len = len * mp[digits.at(i)].length();
            }
            vector<string> rt(len,"");
    
            int index = 0;
            int tp_len = len;
            for( int i = 0;i< digits.length();i++){
                index = 0;
                for( int j = 0;j<len;j++){
                    rt.at(j) = rt.at(j) + mp[digits.at(i)][index];
                    index = (j + 1) / (tp_len/mp[digits.at(i)].length()) % mp[digits.at(i)].length();
                }
                tp_len = tp_len/mp[digits.at(i)].length();
            }
            return rt;
        }
    };
    

    心得:

    对于异常的处理,比如字符串是空串。
    看似简单的组合问题,实际中却并不是特别容易用代码展示出来。必须实现才行。

    相关文章

      网友评论

          本文标题:17. Letter Combinations of a Pho

          本文链接:https://www.haomeiwen.com/subject/dduhcqtx.html