<p>Given a digit string, return all possible letter combinations that the number could represent.</p><p>A mapping of digit to letters (just like on the telephone buttons) is given below.</p>
class Solution {
public:
inline vector<string> comb(vector<string> &a, vector<string> &b)
{
int a_L = a.size();
int b_L = b.size();
vector<string> res;
for (int i = 0; i < a_L; i++)
{
for (int j = 0; j<b_L; j++)
{
res.push_back(a[i] + b[j]);
}
}
return res;
}
vector<string> letterCombinations(string digits) {
vector<vector<string>> tmp(8);
tmp[0] = { "a", "b", "c" };
tmp[1] = { "d", "e", "f" };
tmp[2] = { "g", "h", "i" };
tmp[3] = { "j", "k", "l" };
tmp[4] = { "m", "n", "o" };
tmp[5] = { "p", "q", "r", "s" };
tmp[6] = { "t", "u", "v" };
tmp[7] = { "w", "x", "y", "z" };
int L = digits.size();
vector<string> res;
if (digits[0] >= '2'&&digits[0] <= '9')
{
res=tmp[digits[0] - '0' - 2];
}
for (int i = 0; i < L-1; i++)
{
if (digits[i]>'9' || digits[i]<'2' || digits[i+1]>'9' || digits[i+1]<'2')
{
return{};
}
else
{
vector<string> t;
t.clear();
t = comb(res, tmp[digits[i+1] - '0' - 2]);
res.clear();
res = t;
}
}
return res;
}
};
Letter Combinations of a Phone Number
最新推荐文章于 2020-06-18 06:31:08 发布