zoukankan      html  css  js  c++  java
  • 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.

    class Solution {
        void dsf(string keyStrs[],string& digits,int digitsSize,int pos,vector<string>& res,string &oneOfRes)
        { 
            if(digitsSize == 0){
                return ;
            }
            if(pos==digitsSize){
                res.push_back(oneOfRes);
                return;
            }
            string keyStr=keyStrs[digits[pos] - '0'];
            int keyStrSize = keyStr.size();
            for(int j=0;j<keyStrSize;j++){
                oneOfRes.push_back(keyStr[j]);
                dsf(keyStrs,digits,digitsSize,pos+1,res,oneOfRes);
                oneOfRes.pop_back();
            }
        }
    public:
        vector<string> letterCombinations(string digits) {
            string keyStrs[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
            vector<string> res;
            string oneOfRes;
            int digitsSize = digits.size();
            dsf(keyStrs,digits,digitsSize,0,res,oneOfRes);
            return res;
        }
    };
  • 相关阅读:
    面试题总结
    h5c3新特性
    redis常用命令大全
    windows下挂载linux的nfs网络硬盘
    mysql之char、varchar、text对比
    Lua与C的交互
    通信模型socket
    程序编译流程
    区块链共识机制(POW、POS、DPOS等)的优缺点
    .net c#获取自定义Attribute
  • 原文地址:https://www.cnblogs.com/zengzy/p/5024588.html
Copyright © 2011-2022 走看看