Leetcode Note: Leetcode Letter Combinations of a Phone Number

Source: Internet
Author: User

Leetcode Note: Leetcode Letter Combinations of a Phone Number

I. Description

Given a digit string, return all possible letter combinations that the number coshould 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"].

Ii. Question Analysis

The question is: press a letter on your phone, give the number key you press, and ask all the letters you press. This question can be completed using DFS, which is not difficult.

Iii. Sample Code

Class Solution {public: vector
  
   
Result; vector
   
    
LetterCombinations (string digits) {if (digits = "") return result; static string k [8] = {"abc", "def", "ghi ", "jkl", "mno", "pqrs", "tuv", "wxyz"}; string temp = ""; dfs (digits, temp, k); return result ;} private: void dfs (string dig, string temp, string str []) {// The find function is used to find the first position of a sequence in the string to prevent repeated if (dig. size () = 0 & find (result. begin (), result. end (), temp) = result. end () {result. push_back (temp); return;} else {for (int I = 0; I <str [dig [0]-'2']. size (); ++ I) {temp + = str [dig [0]-'2'] [I]; // Add the last dfs (dig. substr (1, dig. size (), temp, str); temp = temp. substr (0, temp. size ()-1); // remove the last character} return ;}}};
   
  

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.