首页 > 其他分享 >LeetCode电话号码的字母组合(empty/dfs)

LeetCode电话号码的字母组合(empty/dfs)

时间:2023-01-29 12:55:18浏览次数:57  
标签:digits return string combination dfs combinations phoneMap 字母组合 empty

原题解

题目

约束

解法

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> combinations;
        if (digits.empty()) {
            return combinations;
        }
        unordered_map<char, string> phoneMap{
            {'2', "abc"},
            {'3', "def"},
            {'4', "ghi"},
            {'5', "jkl"},
            {'6', "mno"},
            {'7', "pqrs"},
            {'8', "tuv"},
            {'9', "wxyz"}
        };
        string combination;
        backtrack(combinations, phoneMap, digits, 0, combination);
        return combinations;
    }

    void backtrack(vector<string>& combinations, const unordered_map<char, string>& phoneMap, const string& digits, int index, string& combination) {
        if (index == digits.length()) {
            combinations.push_back(combination);
        } else {
            char digit = digits[index];
            const string& letters = phoneMap.at(digit);
            for (const char& letter: letters) {
                combination.push_back(letter);
                backtrack(combinations, phoneMap, digits, index + 1, combination);
                combination.pop_back();
            }
        }
    }
};
class Solution {
public:
    string tmp;
    vector<string> res;
    vector<string> board={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    void DFS(int pos,string digits){
        if(pos==digits.size()){
            res.push_back(tmp);
            return;
        }
        int num=digits[pos]-'0';
        for(int i=0;i<board[num].size();i++){
            tmp.push_back(board[num][i]);
            DFS(pos+1,digits);
            tmp.pop_back();
        }
    }
    vector<string> letterCombinations(string digits) {
        if(digits.size()==0) return res;
        DFS(0,digits);
        return res;
    }
};

标签:digits,return,string,combination,dfs,combinations,phoneMap,字母组合,empty
From: https://www.cnblogs.com/chuixulvcao/p/17072370.html

相关文章

  • 刷刷刷 Day 25 | 17. 电话号码的字母组合
    17.电话号码的字母组合LeetCode题目要求给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按任意顺序返回。给出数字到字母的映射如下(与电话......
  • ubuntu20.04安装fastdfs遇到的问题
    说明:git clone在线安装与离线安装都不成功后,选择原来可以正常运行的fastdfs服务,进行tar打包下载,再在新项目上进行解压部署。但由于打包压缩动态库的软连接失效,所以启动出......
  • 04HDFS简介
    HDFS简介一、什么是HDFSHDFS全称是HadoopDistributedFileSystem,简称HDFS。这是一个分布式文件系统,当数据规模大小超过一台物理计算机的存储能力时,就有必要进行分区并......
  • notepad++ 替换空行 Remove empty lines and spaces in Notepad++?
    RemoveemptylinesandspacesinNotepad++?回答1Togetridofleadingspace(s)andallemptylines(eveniftheemptylinecontainsspacesortabs)Goto S......
  • 图文:深度优先遍历(DFS)和广度优先遍历(BFS)
    /***注意:left/right值若没有显示设置为null,值即为undefined*在调用二叉树前、中、后序遍历方法时,由于参数设置了默认值(tree)*所以进入了死循环*/consttree={......
  • 【算法训练营day25】LeetCode216. 组合总和III LeetCode17. 电话号码的字母组合
    LeetCode216.组合总和III题目链接:216.组合总和III独上高楼,望尽天涯继续复健,一直在犯小的语法错误。慕然回首,灯火阑珊和昨天的题很像,主要区别在于递归返回条件和回溯......
  • Hadoop技术之HDFS工作流程与机制Apache Hadoop概述
    一、HDFS集群角色与职责官方架构图 主角色: namenodeNameNode是Hadoop分布式文件系统的核心,架构中的主角色。NameNode维护和管理文件系统元数据,包括名称空间目录树结构、......
  • POJ--2386 Lake Counting(DFS)
    记录0:332023-1-24http://poj.org/problem?id=3617reference:《挑战程序设计竞赛(第2版)》2.2.3p43DescriptionFJisabouttotakehisN(1≤N≤2,000)cows......
  • dfs爆搜顺序与剪枝
    dfs爆搜顺序与剪枝小猫爬山翰翰和达达饲养了N只小猫,这天,小猫们要去爬山。经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<)。翰翰和达......
  • 算法编程 dfs 从先序和中序遍历还原二叉树
    105.从前序与中序遍历序列构造二叉树给定两个整数数组 preorder和inorder ,其中 preorder是二叉树的先序遍历,inorder 是同一棵树的中序遍历,请构造二叉树并返回......