leetcode | Letter Combinations of a Phone Number
2015-05-28 22:00
302 查看
问题
Letter Combinations of a Phone Number
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.
解析
//时间复杂度 O(3^n), 空间复杂度O(n) ; n为digit的位数 const string key[] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; class Solution { public: vector<string> letterCombinations(string digits) { vector<string> result; if (digits.size() == 0) return result; string str(digits.size(), ' '); //为单个字符串分配空间 PushString(digits, 0, str, result); return result; } private: void PushString(string& digits, int k, string &str, vector<string>& result) { if (k == digits.size()) { result.push_back(str); return; } for (int i = 0; i < key[digits[k] - '0'].size(); i++) { str[k] = key[digits[k] - '0'][i]; //因为每一个str的长度都等于digits的大小,所以str可以重用(完全覆盖) PushString(digits, k + 1, str, result); } } };
相关文章推荐
- C#程序开发中经常遇到的10条实用的代码
- Merge Sorted Array
- Java Servlet学习笔记(五)Servlet服务器Http响应
- 在Visual Studio 的 “一般处理程序 ” .ashx 文件中如何创建Session 对象
- App项目开发流程图解
- 黑马程序员----反射
- iPhone屏幕知识点解析
- A*寻路算法浅析
- [翻译] JHChainableAnimations
- 团队项目 SRS文档
- 一个线程池解决epoll socket队列的方法
- Cpp Primer<<学习函数-局部对象、内联函数、类的成员函数_1
- 一些项目——FatMouse' Trade
- Hadoop的调度器总结
- 第二次冲刺(3)
- 个人日常linux相关工作问题汇总
- data mining - 实用机器学习与技术 读书笔记(七)
- 丑数
- QQ为什么是棵常青树:极客精神+匠人品质
- 轻量级javaEE SSH 01: 概述 环境与工具篇