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.
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
水题: DFS
int counts[] = {, , , , , , , , , };
char letter[] = {'', '', 'a', 'd', 'g', 'j', 'm', 'p', 't', 'w'}; class Solution {
public:
void DFS( string & digit,int i, string s)
{
if(i == size){
result.push_back(s);
return ;
}
int index = digit[i] - '' ; for(int j = ; j < counts[index] ; j++)
{ char c = letter[index]+j ; DFS(digit, i+, s+c); } }
vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
size = digits.size(); string s = "";
DFS(digits, , s); return result ; }
private :
vector<string> result ;
int size ;
};