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 };
  • 相关阅读:
    robotframework-requests--中文注解版
    Python猜数小游戏
    走进Selenium新世界
    HTML
    Web测试方法_02
    3.线程死锁
    2.线程--线程安全(synchronized)
    1.线程--线程创建方式
    使用Android-studio开发移动app与weex结合开发详细步骤
    Weex 简介
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4873347.html
Copyright © 2011-2022 走看看