17. Letter Combinations of a Phone Number
来源:互联网 发布:确认密码 javascript 编辑:程序博客网 时间:2024/06/11 04:06
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.
给出一个字符串,代表数字按键顺序,求出所有可能的字母按键顺序。这里使用队列来实现。具体实现见代码:
代码:
class Solution {public: vector<string> letterCombinations(string digits) { vector<string> res; if(digits.empty() || digits == "") return res; queue<string> q; q.push(""); for(int i = 0; i < digits.size(); ++i) { string chars = getChars(digits[i]); int qs = q.size(); for(int j = 0; j < qs; ++j) { string s = q.front(); q.pop(); for(auto c:chars) { q.push(s+c); } } } while(!q.empty()) { res.push_back(q.front()); q.pop(); } return res; }private: string getChars(char d) { vector<string> map{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; return map[d-'0']; }};
阅读全文
0 0
- 17.Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 程序实现汉字转换为拼音
- POJ3068--"Shortest" pair of paths(最小费用流)
- DigitalOcean 更换IP
- linux各种锁机制的使用及区别(浅谈)
- 预处理、结构体10个问答
- 17. Letter Combinations of a Phone Number
- 胡策 zhxP97 a
- 必须为元素类型 "mapper" 声明属性 "namespace" 或The content of element type "mapper" must match "EMPTY"
- 【微信小程序】--bindtap参数传递,配合wx.previewImage实现多张缩略图预览
- 再议Java/Android回调——由一段Bmob查询操作引发的思考
- 位运算
- Opencv 2.4.9在Ubuntu下的配置安装
- Java中的overload和override的区别
- 初学Qt:QLineEdit设置为不可编辑