LeetCode 17 Letter Combinations of a Phone Number (Phone Number combination)

Source: Internet
Author: User

LeetCode 17 Letter Combinations of a Phone Number (Phone Number combination)
Translation

Returns a combination of letters that can be expressed by a given number string. Shows the ing of a number to a letter (just like a telephone button. Input: numeric string "23" output: [ad, AE, af, bd, be, bf, cd, ce, cf] remarks: although the above answers are unordered, if you want to, your answers can be ordered.
Source image

Original
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 23Output: [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.
Code

It looks like I still use C # To get it in one breath ...... It mainly uses recursion because I don't know Digits Cannot Write countless Foreach To judge.

public class Solution{    IList
  
    list = new List
   
    ();    public Solution()    {        list.Insert(0, abc);        list.Insert(1, def);        list.Insert(2, ghi);        list.Insert(3, jkl);        list.Insert(4, mno);        list.Insert(5, pqrs);        list.Insert(6, tuv);        list.Insert(7, wxyz);    }    public IList
    
      LetterCombinations(string digits)    {        IList
     
       result = new List
      
       (); if (digits.Length == 0) return result; if (digits.Length == 1) { foreach (var a in list.ElementAt(int.Parse(digits[0].ToString()) - 2)) { result.Insert(0, a.ToString()); } } int count = 0; IList
       
         temp = LetterCombinations(digits.Substring(1, digits.Length - 1)); foreach (var a in list.ElementAt(int.Parse(digits[0].ToString()) - 2)) { foreach (var rest in temp) { result.Insert(count++, a.ToString() + rest); } } return result; }}
       
      
     
    
   
  

The following is a copy of C ++ code. Frankly speaking, I cannot write such C ++ code ......

class Solution {public:    vector
  
    letterCombinations(string digits) {                vector
   
     ans;        if(digits.size() == 0)            return ans;        int depth = digits.size();        string tmp(depth, 0);        dfs(tmp, 0, depth, ans, digits);        return ans;    }    void dfs(string &tmp, int curdep, int depth, vector
    
      &ans, string &digits){        if(curdep >= depth){            ans.push_back(tmp);            return ;        }        for(int i = 0; i < dic[digits[curdep] - '0'].size(); ++ i){            tmp[curdep] = dic[digits[curdep] - '0'][i];            dfs(tmp, curdep + 1, depth, ans, digits);        }        return ;    }private:string dic[10] = {{},{},{abc},{def},{ghi},{jkl},{mno},{pqrs},{tuv},{wxyz}};};
    
   
  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.