17. Letter Combinations of a Phone Number

Description

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"].


Note: Although the above answer is in lexicographical order, your answer could be in any order you want.

code

class Solution {
private:
void backtracking(vector<string> & result,string & temp,string & digits,int start,int * map){
if(start==digits.size()){
result.push_back(temp);
return;
}
int tmp=digits[start]-'0'-2;
for(int i=map[tmp];i<map[tmp+1];++i){
temp.push_back('a'+i);
backtracking(result,temp,digits,start+1,map);
temp.pop_back();
}
return;
}
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
string temp="";
int map[10]={0,3,6,9,12,15,19,22,26};
backtracking(result,temp,digits,0,map);
return result;
}
};


学籍管理系统文档

北邮教务系统评教脚本

Published on September 17, 2017

72.Edit distance

Published on September 17, 2017