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 ;}}};