您的位置:首页 > 其它

[Leetcode 47] 17 Letter Combinations of a Phone Number

2013-05-25 13:08 429 查看
Problem:

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.

class Solution {
public:
vector<string> table;
vector<string> res;

vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (digits.size() == 0)
return vector<string>(1,"");

res.clear();
table.resize(10);
table[2] = "abc";
table[3] = "def";
table[4] = "ghi";
table[5] = "jkl";
table[6] = "mno";
table[7] = "pqrs";
table[8] = "tuv";
table[9] = "wxyz";

backtrack(digits, 0, "");
return res;
}

void backtrack(string &s, int pos, string ss) {
if (pos == s.size()) {
res.push_back(ss);
return ;
}

for (int i=0; i<table[s[pos] - '0'].size(); i++) {
backtrack(s, pos+1, ss+table[s[pos] - '0'][i]);
}

return ;
}
};


View Code

Attention:

There are many C++ skills used in this code, learn it.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: