LeetCode刷题记录——17电话号码的字母组合
LeetCode刷题记录——17电话号码的字母组合
一 题目描述:
给定一个仅包含数字2-9的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下。注意1不对应任何字母
示例:
输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
二 思路:
递归遍历:
如图从最底层开始,然后逐层往上进行递归调用,例如,对于打印"2345"这样的字符串:
第一次递归就是上图中最下面的方格,然后处理完第一个字符2之后,将输入的字符改变成"345"并调用第二个递归函数
第二次递归处理3,将字符串改变成"45"后再次递归
第三次递归处理4,将字符串改变成"5"后继续递归
第四次递归处理5,将字符串改变成""后继续递归
最后发现字符串为空了,将结果放到列表中并返回代码:
vector<string> table = {"abc","def","ghi","jkl","mno","qprs","tuv","wxyz"};void func(vector<string> &res,string str,string &digits,int i){if(i>=digits.size()){ //跳出条件,结合图理解,文字不好描述if(str.size() > 0 ){res.push_back(str);}return ;}else{string s = table[digits[i] - '2']; //得到当前的数字对应的字母串for(int j = 0;j<s.size();j++){ //递归调用,结合上图理解str.push_back(s[j]);func(res,str,digits,i+1);str.pop_back();}}}vector<string> letterCombinations(string digits){vector<string> res;func(res,"",digits,0);return res; }
队列:
先把第一个数字对应的字母串放在队列头,比如23,就先把abc放在按照顺序放在队列中,然后从队列中拿出第一个a,与3对应的字母串拼接,同样的方式,讲b与3对应的字母串拼接,直到结束
代码
vector<string> letterCombinations(string digits){vector<string> res; //用于输出向量//映射map哈希表map<char,string> m = { {'2',"abc" },{'3',"def"},{'4',"ghi"},{'5',"jkl"},{'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"} };int size = digits.size();queue<string> que;//将第一个数字对应的字母串入队列for(int j = 0;j<m[digits[0]].size();j++){string str;str.push_back(m[digits[0]][j]);que.push(str);}string s ; //存储队列头元素for(int i = 1;i < size;i++){int length = que.size();//当前队列的长度while(length--){for(int j = 0;j<m[digits[i]].size();j++){s = que.front();s = s+m[digits[i]][j];//队列头元素加上新元素que.push(s);}que.pop();}}while(!que.empty()){res.push_back(que.front());que.pop();}return res; }
LeetCode刷题记录——17电话号码的字母组合相关推荐
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...
- LeetCode刷题记录9——58. Length of Last Word(easy)
LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...
- LeetCode刷题记录8——605. Can Place Flowers(easy)
LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...
- LeetCode刷题记录7——824. Goat Latin(easy)
LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...
最新文章
- asp.net mysql 读写分离_MySQL读写分离
- 架构师之路 — 分布式系统 — 分布式事务难题
- pytorch基础知识整理(二)数据加载
- 敲黑板!vue3重点!一文了解Composition API新特性:ref、toRef、toRefs
- [转]SQL Server 阻止了对组件 /'Ad Hoc Distributed Queries/' 的访问
- 实习成长之路:MySQL十:怎么给字符串字段加索引?
- linux下查看文件描述符,linux下文件描述符的查看及分析
- 青苹果linux版本客户端,青苹果影院新版本
- 【原】[webkit移动开发笔记]之无法自动播放的audio元素
- Pytorch基础操作 —— 8. 张量转置操作
- sql server 数据库正在使用该文件的解决办法
- Codeforces--44A--Indian Summer
- 在visual studio 2019中使用msvc2015
- 数据库实验九2021011206贾天乐
- 读书笔记(I) 游戏之旅
- 远程连接桌面到ubuntu登录闪退
- houdini 太阳制作
- 1461:Beads
- 光电接收的TIA设计
- 管理学定律一:蝴蝶效应与青蛙现象