zoukankan      html  css  js  c++  java
  • 45-Letter Combinations of a Phone Number

    1. Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution
      Total Accepted: 78554 Total Submissions: 273286 Difficulty: Medium
      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”].

    Submission Details
    25 / 25 test cases passed.
    Status: Accepted
    Runtime: 0 ms

    思路:依次迭代,比较简单

    class Solution {
    public:
        vector<string> letterCombinations(string digits) {
            int n = digits.size();
            vector<string> res;
            map<char,string> nmap;
            init_map(nmap);
            for(int i=0;i<n;++i){
                if(digits[i]=='1')continue;//1代表空,无效数字越过
                string word= nmap[digits[i]];
                vector<string> sw,stmp;
                for(int j=0;j<word.size();++j){
                    vector<char> vs;
                    vs.push_back(word[j]);
                    vs.push_back('');         //特别注意char* 转string
                    sw.push_back(vs.data());
                    for(int k=0;k<res.size();++k)
                        stmp.push_back(res[k]+sw[j]);
                }
                if(res.size()==0)res = sw;
                else res = stmp;
            }
            return res;
        }
        void init_map(map<char,string> &nmap)
        {
            nmap['1']="";nmap['2']="abc";nmap['3']="def";
            nmap['4']="ghi";nmap['5']="jkl";nmap['6']="mno";
            nmap['7']="pqrs";nmap['8']="tuv";nmap['9']="wxyz";
            nmap['0']=" ";
    
        }
    };
  • 相关阅读:
    PHP如何采集网站数据
    十一. Go并发编程singleflight
    2. Go中defer使用注意事项
    九. Go并发编程context.Context
    3. Go中panic与recover注意事项
    Go 性能提升tips边界检查
    十.Go并发编程channel使用
    八. Go并发编程errGroup
    十二. Go并发编程sync/errGroup
    Go知识盲区闭包
  • 原文地址:https://www.cnblogs.com/freeopen/p/5482921.html
Copyright © 2011-2022 走看看