LeetCode Letter Combinations of a Phone Number
来源:互联网 发布:abb机器人编程视频 编辑:程序博客网 时间:2024/06/01 10:48
这题貌似简单,我却想了很久,感觉不太容易上手,要是面试时肯定不能一下子就写出来。我的思路:后面的数字每到最大长度,前面的就进位(即前面位加一,后面的位变为0)知道最前面的位到达它的长度,此时结束。
// LeetCode_LetterPhoneNumber.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <vector>#include <string>#include <map>using namespace std;vector<string> letterCombinations(string digits) {map<char,string> mp;mp['0']="";mp['1']="";mp['2']="abc";mp['3']="def";mp['4']="ghi";mp['5']="jkl";mp['6']="mno";mp['7']="pqrs";mp['8']="tuv";mp['9']="wxyz";string tempstr="";vector<string> ret;int lendig = digits.length();if (lendig==0){ret.push_back(tempstr);return ret;}int *pnum = new int[lendig];memset(pnum,0,lendig*sizeof(int));//while(pnum[0]<mp[digits[0]].length()){int bit=0;while(bit<lendig){tempstr += mp[digits[bit]][pnum[bit]];bit++;}ret.push_back(tempstr);tempstr.clear();int inbit = lendig - 1;pnum[inbit]++;while(pnum[inbit] >= mp[digits[inbit]].length()){if (pnum[0]==mp[digits[0]].length())break;pnum[inbit] = 0;pnum[inbit-1]++;inbit--;}}delete[] pnum;return ret;}int _tmain(int argc, _TCHAR* argv[]){vector<string> vec;vec = letterCombinations("237");for (int i=0;i<vec.size();i++){cout<<vec[i]<<" ";}system("pause");return 0;}
//输入不合理时,不能输出正确的结果,之前要对输入进行过滤。可以看出,leetCode的编译器的测试用例还是比较简单的,没有对负面测试进行处理。所有测试用例都保证有效。
别人的解法,干净利落呀,望尘莫及。看都没看懂。如下:
vector<string> letterCombinations(string digits) { // Start typing your C/C++ solution below // DO NOT write int main() function const string letters[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string> ret(1, ""); for (int i = 0; i < digits.size(); ++i) { for (int j = ret.size() - 1; j >= 0; --j) { const string &s = letters[digits[i] - '2']; for (int k = s.size() - 1; k >= 0; --k) { if (k) ret.push_back(ret[j] + s[k]); else ret[j] += s[k]; } } } return ret; }这题还有递归的做法,别人的。递归算法倒是好理解,每次选一个当前数字对应的字母,下次选择下一个数字对应的字母,当选择数量达到给定数字串长度时作为一个结果加入到结果集里。如下:
void letterCombinations_aux(int step, string& path, vector<string>& ans, const string& digits) { //pay attention to this kind of statement const static string strT[10] = {"","","abc","def","ghi","jkl","mno","qprs","tuv","wxyz"}; if(step == digits.size()) { ans.push_back(path); return; } for(int i = 0; i < strT[digits[step]-'0'].size(); ++i) { path.push_back(strT[digits[step]-'0'][i]); letterCombinations_aux(step+1, path, ans, digits); path.pop_back(); } } vector<string> letterCombinations(string digits) { // Start typing your C/C++ solution below // DO NOT write int main() function string path; vector<string> ans; int step = 0; letterCombinations_aux(step, path, ans, digits); return ans; }
- 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
- 新知识的遗忘数度真是可怕
- Linux(五) Vi/Vim 基础入门
- Sample 5.1:forloop.cpp
- 关于Thinkpad的恢复盘
- c#创建快捷方式到快速启动栏
- LeetCode Letter Combinations of a Phone Number
- Sample 5.2:num_test.cpp
- The mathematics in computer vision and machine learning
- windows线程同步方法
- Sample 5.3:express.cpp
- Sample 5.4: formore.cpp
- GoLang之Concurrency协程goroutine使用方法
- mvc适用多大的工程
- Sample 5.5: bigstep.cpp