NC329. 电话号码的字母组合
描述
示例1
输入:
"55"
输出:
["JJ","JK","JL","KJ","KK","KL","LJ","LK","LL"]
示例2
输入:
"3"
输出:
["D","E","F"]
C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-03-07
class Solution { string arr[10]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num string字符串 * @return string字符串vector */ void _phone(const string& digits,int i,string combinstr,vector<string>&strv) { if(i==digits.size()) { strv.push_back(combinstr); return; } string str=arr[digits[i]-'0']; for(int j=0;j<str.size();++j) { _phone(digits,i+1,combinstr+str[j],strv); } } vector<string> phoneNumber(string num) { // write code here string combinstr; vector<string> strv; if(num.empty()) { return strv; } _phone(num,0,combinstr,strv); return strv; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2022-07-29
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num string字符串 * @return string字符串vector */ string arr[10]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; string tmp; vector<string> ans; vector<string> phoneNumber(string num) { // write code here if(num.size()==0) { return ans; } backtrack(num,0); return ans; } void backtrack(string &num,int pos) { if(pos>=num.size()) { ans.push_back(tmp); return ; } int n = num[pos]-'0'; for(int i=0;i<arr[n].size();i++) { tmp+=arr[n][i]; backtrack(num, pos+1); tmp.pop_back(); } } };
C++ 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2022-06-29
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num string字符串 * @return string字符串vector */ const vector<string> lettermap={ "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz", }; vector<string> res; vector<string> phoneNumber(string num) { // write code here string s=""; backtracing(num,s,0); return res; } void backtracing(string& num,string& s,int index){ if(index==num.size()){ res.push_back(s); return; } int digit=num[index]-'0'; for(int i=0;i<lettermap[digit].size();++i){ s.push_back(lettermap[digit][i]); backtracing(num,s,index+1); s.pop_back(); } } };
C++ 解法, 执行用时: 3ms, 内存消耗: 416KB, 提交时间: 2022-07-11
class Solution { vector<vector<char>>map={ {'A','B','C'}, {'D','E','F'}, {'G','H','I'}, {'J','K','L'}, {'M','N','O'}, {'P','Q','R','S'}, {'T','U','V'}, {'W','X','Y','Z'} }; public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num string字符串 * @return string字符串vector */ void dfs(vector<string>& ans,string t,string num,int i){ if(i==num.length())ans.push_back(t); else{ for(char c:map[num[i]-'2'])dfs(ans,t+c,num,i+1); } } vector<string> phoneNumber(string num) { // write code here vector<string>ans; dfs(ans,"",num,0); return ans; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 416KB, 提交时间: 2022-03-03
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num string字符串 * @return string字符串vector */ vector<string>ans; string tmp; const vector<string>dic={"ABC","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector<string> phoneNumber(string digits) { // write code here int len=digits.size(); if(!len) return ans; backtrack(digits,0); reverse(ans.begin(),ans.end()); return ans; } void backtrack(string &digits,int pos){ if(pos>=digits.size()){ ans.emplace_back(tmp); return; } int num=digits[pos]-'2'; for(char c:dic[num]){ tmp+=c; backtrack(digits,pos+1); tmp.pop_back(); } } };