[LeetCode] Letter Combinations of a Phone Number
来源:互联网 发布:软件授权使用说明书 编辑:程序博客网 时间:2024/06/08 17:50
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-9数字中的任意几个,其中每个数字都包含三个字母,其所包含的字母如上图所示,求出这些数字所构成的所有组合。
该种题型较为简单,我们可以通过深度优先遍历求解。
深度优先遍历算法如下:
复杂度:时间 O(n2)递归栈空间
思路:首先建一个表,来映射号码和字母的关系。然后对号码进行深度优先搜索,对于每一位,从表中找出数字对应的字母,这些字母就是本轮搜索的几种可能。
char str[1000];vector<string>result;class Solution {public: vector<string> letterCombinations(string digits) { int len = digits.size(); if (len == 0) return result; string nums[] = {" ", " ", "ab", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; digitsDeal(digits, 0, len, nums); return result; }public: void digitsDeal(string &digits, int i, int len, string *nums) { if(i == len) { str[len] = '\0'; string temp = str; result.push_back(temp); return; } int index = digits[i]-'0'; for(int j= 0; j < nums[index].size(); ++j) { str[i] = nums[index][j]; digitsDeal(digits, i+1, len, nums); } }};
阅读全文
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
- 8.4.2 时间序列预测——使用TFLearn自定义模型——代码运行错误及解决方法
- 2---python开发简单爬虫
- 深度学习论文roadmap
- 一点幼稚的记录
- C语言笔记(符号)
- [LeetCode] Letter Combinations of a Phone Number
- Hibernate的学习之路十九(一对多的JavaBean编写和映射的配置文件的编写)
- 【题解】【UVA 11297】Census
- ps----将人物或别的东西放进小球
- 如何修改eclipse下tomcat的工程所生成的项目文件的名称
- Android viewpager嵌套viewpager滑动冲突的解决
- (有参)构造函数
- 直线扫描转换算法-中点画线法
- 基于SSM框架的教务查询系统实践(1)—— 项目整体功能概述