leetcode 17. Letter Combinations of a Phone Number
来源:互联网 发布:淘宝怎样退换货 编辑:程序博客网 时间:2024/05/21 10:09
1.题目
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.
2.思路
1)有两个digits时,例如“23“,那么相应的字符为”a,b,c“,”d,e,f“。
两层循环,构成组合"ad","ae","af","bd","be","bf","cd","ce","cf"
2)有大于两个digits时,例如”234“,
那么先将前两个数字”23“,利用 1)中的方法,组合,结果存为result。
再将result和下一个数字 ,再利用 1)的方法,组合,直至数字字符串结束。
class Solution {public: vector<string> letterCombinations(string digits) { vector<string>result; number['2']={'a','b','c'}; number['3']={'d','e','f'}; number['4']={'g','h','i'}; number['5']={'j','k','l'}; number['6']={'m','n','o'}; number['7']={'p','q','r','s'}; number['8']={'t','u','v'}; number['9']={'w','x','y','z'}; if(digits.length()==0) return result; for(int i=0;i<digits.length();i++) help(result,digits[i]); return result; } void help(vector<string>&result,char digit){ vector<string>temp; if(result.size()==0){ for(int j=0;j<number[digit].size();j++) {string tt; tt=tt+number[digit][j]; temp.push_back(tt);} result=temp; return; } for(int i=0;i<result.size();i++){ for(int j=0;j<number[digit].size();j++) temp.push_back(result[i]+number[digit][j]); } result=temp; } private: unordered_map<char,vector<char>>number;};
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
- PopupWindow 注意点
- C#调用dll提示"试图加载格式不正确的程序"原因及解决方法
- 新手学习wpf记录 button上篇 等级3
- android中的线程池的使用
- jvm command 小计
- leetcode 17. Letter Combinations of a Phone Number
- MySql的常用命令和增删改查语句和数据类型
- MySQL的JDBC判断查询结果是否为空以及获取查询结果行数的方法
- COM组件调用过程
- python遍历字典所有的字段
- Silverlight中非对称加密及数字签名RSA算法的实现
- Quartz 触发器(SimpleTrigger&CronTrigger )配置说明 & cronExpression表达式
- POJ_3281_Dining && NYOJ _326_Dining
- const,static,extern简介