• LeetCode OJ-- Letter Combinations of a Phone Number ***


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

    使用递归,深搜,使用 map 保存已经处理过的结果

    class Solution {
    public:
        unordered_map<string, vector<string> > memory;
        
        vector<string> letterCombinations(string digits) {
            vector<string> ans;
            
            if(digits.size() == 0)
            {
                ans.push_back("");
                return ans;
            }
            
            memory.clear();
            
            // initialize
            vector<string> piece;
            piece.push_back("a");
            piece.push_back("b");
            piece.push_back("c");
            memory.insert(make_pair("2",piece));
            
            vector<string> piece3;
            piece3.push_back("d");
            piece3.push_back("e");
            piece3.push_back("f");
            memory.insert(make_pair("3",piece3));
            
            vector<string> piece4;
            piece4.push_back("g");
            piece4.push_back("h");
            piece4.push_back("i");
            memory.insert(make_pair("4",piece4));
            
            vector<string> piece5;
            piece5.push_back("j");
            piece5.push_back("k");
            piece5.push_back("l");
            memory.insert(make_pair("5",piece5));
            
            vector<string> piece6;
            piece6.push_back("m");
            piece6.push_back("n");
            piece6.push_back("o");
            memory.insert(make_pair("6",piece6));
            
            vector<string> piece7;
            piece7.push_back("p");
            piece7.push_back("q");
            piece7.push_back("r");
            piece7.push_back("s");
            memory.insert(make_pair("7",piece7));
            
            vector<string> piece8;
            piece8.push_back("t");
            piece8.push_back("u");
            piece8.push_back("v");
            memory.insert(make_pair("8",piece8));
            
            vector<string> piece9;
            piece9.push_back("w");
            piece9.push_back("x");
            piece9.push_back("y");
            piece9.push_back("z");
            memory.insert(make_pair("9",piece9));
            
            subLetter(digits, ans);
            return ans;
        }
        
        void subLetter(string &digits, vector<string> &ans)
        {
            // already in 
            if(memory.find(digits) != memory.end())
            {
                ans = memory[digits];
                return;
            }
            
            // split digits
            int mid = digits.size()/2;
            string former = digits.substr(0,mid);
            string latter = digits.substr(mid,digits.size() - mid);
            
            vector<string> strsFormer;
            vector<string> strsLatter;
            if(memory.find(former) != memory.end())
                strsFormer = memory[former];
            else
                subLetter(former, strsFormer);
            
            if(memory.find(latter) != memory.end())
                strsLatter = memory[latter];
            else
                subLetter(latter,strsLatter);
            
            for(int i = 0; i < strsFormer.size(); i++)
                for(int j = 0; j < strsLatter.size(); j++)
                {
                    string temp = strsFormer[i] + strsLatter[j];
                    ans.push_back(temp);
                }
            
            memory.insert(make_pair(digits,ans));
            return;
        }
        
    };
  • 相关阅读:
    NodeJS、NPM安装配置步骤(windows版本)
    23种设计模式全解析
    js阻止浏览器默认事件
    js获取不同浏览器盒子宽度高度
    H5之重力感应篇
    JS中的call()和apply()方法
    html学习笔记
    less(css)语言快速入门
    power designer简单教程
    Strom开发配置手册
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3893221.html
Copyright © 2020-2023  润新知