zoukankan      html  css  js  c++  java
  • 17. Letter Combinations of a Phone Number

    Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

    Example:

    Input: "23"
    Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
    
    思考:按照如下图进行遍历。以输入为“234”为例。
     
     1 class Solution {
     2 public:
     3     vector<string> letterCombinations(string digits) {
     4         vector<string> res;
     5         if(digits.size()==0) return res;
     6         
     7         string map[] = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
     8         string comb(digits.size(),'');
     9         recursion(digits, 1, comb, res,map);
    10         return res;
    11     }
    12     void recursion(string &digits, int len, string &comb, vector<string> &res, string map[]) {
    13         if(len==digits.size()+1) {
    14             res.push_back(comb);
    15             return;
    16         }
    17         
    18         string letters = map[digits[len-1] - '0'];
    19         for(int j=0; j<letters.size(); j++) {
    20             comb[len-1] = letters[j];
    21             recursion(digits, len+1, comb, res,map);
    22         }
    23     }
    24 };
  • 相关阅读:
    Linux -- 查看是否安装了指定的包
    linux -- 部署java服务器(1) linux安装jdk
    spring boot -- 接收文件接口
    vue3 --相对于vue2的改变T1档次
    243交换输出
    24416进制的简单运算
    7街区最短路径问题
    206矩形的个数
    33蛇形填数
    273字母小游戏
  • 原文地址:https://www.cnblogs.com/midhillzhou/p/8873210.html
Copyright © 2011-2022 走看看