[leetcode] 17. Letter Combinations of a Phone Number

网友投稿 855 2022-08-23 16:45:02

[leetcode] 17. Letter Combinations of a Phone Number

Description

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:

Input: "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.

分析

题目的意思是:给定一串数字,然后求出所有的相应字母(如图)的字符串组合。

就是一个深度优先搜索的问题,要注意循环函数的参数以及递归终止条件就行了。其他的没什么难度。

代码

class Solution {public: vector letterCombinations(string digits) { vector res; if(digits.empty()){ return res; } string dict[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; dfs(digits,0,dict,res,""); return res; } void dfs(string digits,int level,string dict[],vector &res,string out){ if(level==digits.size()){ res.push_back(out); }else{ string s=dict[digits[level]-'2']; for(int i=0;i

参考文献

​​[编程题]letter-combinations-of-a-phone-number​​​​[LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[leetcode] 1282. Group the People Given the Group Size They Belong To
下一篇:[leetcode] 300. Longest Increasing Subsequence
相关文章