[leetcode] 17. Letter Combinations of a Phone Number
来源:互联网 发布:mac qq不能接收文件夹 编辑:程序博客网 时间:2024/05/21 00:18
Given a digit string, 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.
Input:Digit string "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.
这道题是求出一串数字按键可能代表的字符串,缅怀一下Nokia,题目难度为Medium。
题目本身比较简单,典型的递归回溯题目,具体就不详细说明了,对回溯法还不太熟悉的同学请查看八皇后题目(传送门),具体代码:
class Solution { void getLetters(vector<string>& rst, string curRst, const string& digits, int pos) { if(pos == digits.size()) { rst.push_back(curRst); return; } vector<char> letter(4, 'a'); int cnt = 0; switch(digits[pos]) { case '2': { cnt = 3; letter[0] = 'a'; letter[1] = 'b'; letter[2] = 'c'; break; } case '3': { cnt = 3; letter[0] = 'd'; letter[1] = 'e'; letter[2] = 'f'; break; } case '4': { cnt = 3; letter[0] = 'g'; letter[1] = 'h'; letter[2] = 'i'; break; } case '5': { cnt = 3; letter[0] = 'j'; letter[1] = 'k'; letter[2] = 'l'; break; } case '6': { cnt = 3; letter[0] = 'm'; letter[1] = 'n'; letter[2] = 'o'; break; } case '7': { cnt = 4; letter[0] = 'p'; letter[1] = 'q'; letter[2] = 'r'; letter[3] = 's'; break; } case '8': { cnt = 3; letter[0] = 't'; letter[1] = 'u'; letter[2] = 'v'; break; } case '9': { cnt = 4; letter[0] = 'w'; letter[1] = 'x'; letter[2] = 'y'; letter[3] = 'z'; break; } default: break; } for(int i=0; i<cnt; i++) { getLetters(rst, curRst+letter[i], digits, pos+1); } }public: vector<string> letterCombinations(string digits) { vector<string> rst; if(digits.empty()) return rst; getLetters(rst, "", digits, 0); return rst; }};
0 0
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- LeetCode | Letter Combinations of a Phone Number
- leetcode Letter Combinations of a Phone Number
- Leetcode: Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- css改变下拉列表select框的默认样式
- cookie中存、取json格式的数据
- mysql的学习笔记
- Android 深入探讨sigleTask和singletop的区别
- 【OpenCV入门指南】第四篇 图像的二值化
- [leetcode] 17. Letter Combinations of a Phone Number
- PowerBuilder命令行编译详解[1]
- MySQL创建用户与授权方法
- Thread线程类的详细解释
- A股熔断后,群体恐慌诱发磁吸效应
- CocoaPods私有库创建遇到的问题
- Eclipse中.setting目录下文件介绍
- 【OpenCV入门指南】第五篇 轮廓检测
- beego