LeetCode 17. 电话号码的字母组合(回溯)
发布日期:2021-07-01 03:13:58 浏览次数:2 分类:技术文章

本文共 1093 字,大约阅读时间需要 3 分钟。

文章目录

1. 题目信息

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

在这里插入图片描述

示例:输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

说明:

尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

2. 解题

类似题目:

  • 先创建哈希映射表
  • 然后暴力回溯

在这里插入图片描述

class Solution {
public: vector
letterCombinations(string digits) {
if(digits.empty()) return {
}; string temp; vector
ans; unordered_map
> v; v['2'] = {
'a','b','c'}; v['3'] = {
'd','e','f'}; v['4'] = {
'g','h','i'}; v['5'] = {
'j','k','l'}; v['6'] = {
'm','n','o'}; v['7'] = {
'p','q','r','s'}; v['8'] = {
't','u','v'}; v['9'] = {
'w','x','y','z'}; bt(v,digits,temp,ans,0); return ans; } void bt(unordered_map
> &v, string &digits, string temp, vector
&ans, int i) { if(i == digits.size()) { ans.push_back(temp); return; } vector
ch = v[digits[i]]; for(auto it = ch.begin(); it != ch.end(); ++it) { temp.push_back(*it); bt(v,digits,temp,ans,i+1); temp.pop_back(); } }};

转载地址:https://michael.blog.csdn.net/article/details/100436281 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode 557. 反转字符串中的单词 III(栈)
下一篇:LeetCode 35. 搜索插入位置(二分查找)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月17日 11时10分23秒