Leetcode Letter Combinations of a Phone Number DFS
来源:互联网 发布:sql注入测试工具汇总 编辑:程序博客网 时间:2024/05/29 12:14
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
有一个trick 就是如果是输入"" 这个时候返回空
其他就是代码写的简洁的问题了
#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <vector>using namespace std;const int SIZE = 10+3;class Solution {public: inline string getString(int num) { switch (num) { case 0 : return " "; case 1: return ""; case 2: return "abc"; case 3: return "def"; case 4: return "ghi"; case 5: return "jkl"; case 6: return "mno"; case 7: return "pqrs"; case 8: return "tuv"; case 9: return "wxyz"; } return ""; } void dfs(string digits, int ptr, string now) { if(ptr >= digits.size() && now.size()==digits.size()){ ans.push_back(now); return; } for(int i=ptr;i<digits.size();i++) { string str = getString(digits[i]-'0'); for(int j=0;j<str.size();j++) { dfs( digits, i+1, now+str[j] ); } } } vector<string> letterCombinations(string digits) { ans.clear(); if(digits.size() == 0)return ans; //WA了一次 dfs(digits, 0, ""); return ans; }private: vector <string> ans;};int main() { Solution s; int n; string digits; while(cin >> digits) { vector<string> ans = s.letterCombinations(digits); for(int i=0;i<ans.size();i++) { cout << ans[i] << ", "; } cout << endl; } return 0;}
一个漂亮的代码
#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <vector>using namespace std;class Solution {public: vector<string> letterCombinations(string digits) { mapping = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string> ret; if(digits.size() == 0)return ret; letterCombinationsCore(digits, "", ret); return ret; } void letterCombinationsCore(string digits, string sub, vector<string> &ret) { if (sub.size() == digits.size()) { ret.push_back(sub); } else { int i = digits[sub.size()] - '0'; for (int j = 0; j < mapping[i - 2].size(); ++j) { sub.push_back(mapping[i - 2][j]); letterCombinationsCore(digits, sub, ret); sub.pop_back(); } } } vector<string> mapping;};
0 0
- Leetcode dfs Letter Combinations of a Phone Number
- Leetcode Letter Combinations of a Phone Number DFS
- LeetCode Letter Combinations of a Phone Number DFS
- LeetCode OJ-17.Letter Combinations of a Phone Number(DFS)
- 【DFS】Letter Combinations of a Phone Number
- DFS 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
- 关系数据库中关系表间的连接(左连接,右连接,内连接,全连接,外连接)
- CDOJ 1061 秋实大哥与战争 暴力/set
- C++11中emplace_back和push_back的区别
- Win7 Qt4.8.5+QtCreator2.8.0+mingw配置过程
- Hadoop 下一代 MapReduce - 集群参数设置与启动
- Leetcode Letter Combinations of a Phone Number DFS
- 测试
- leetcode: Search a 2D Matrix II
- leetcode (136) Single Number js代码实现
- 2015年终小记---------暗流涌动浪淘沙,千金难买一横眉
- ?super T 和? extends T区别
- C++命名空间
- 【APUE】7、signal函数
- 数字图像处理图像增强技术