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.

    Analyse: Be careful about the difference between empty string and non-empty string when conducting an operation. If you set an empty string, you don't have an access to any index, the only operation you can do is contat/'+'. 

    Runtime: 4ms

     1 class Solution {
     2 public:
     3     vector<string> letterCombinations(string digits) {
     4         vector<string> result;
     5         if(digits.empty()) return result;
     6         
     7         string temp(digits.size(), 0);
     8         helper(digits, result, temp, 0);
     9         return result;
    10     }
    11     
    12     void helper(string digits, vector<string>& result, string temp, int depth){
    13         if(depth == digits.size()){
    14             result.push_back(temp);
    15             return;
    16         }
    17         
    18         string info[] = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    19         string current = info[digits[depth] - '0']; //the size of the current string that the digit represent
    20         for(int i = 0; i < current.size(); i++){
    21             temp[depth] = current[i];
    22             helper(digits, result, temp, depth + 1);
    23         }
    24     }
    25 };
  • 相关阅读:
    汇编学习笔记(一)
    外部中断的资料
    喇叭的落幕
    红外模块
    SQL2005连接不上解决
    DataGrid中动态添加列,使用CheckBox选择行
    List和ObservableCollection的相互转化
    使用C#发送邮件
    C#委托与事件 简明
    Linq GroupBy 求和
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4873347.html
Copyright © 2011-2022 走看看