Redian新闻
>
问一下prefix tree (trie) 的题目
avatar
问一下prefix tree (trie) 的题目# JobHunting - 待字闺中
h*d
1
“手机上给了电话号码求人名索引”
谁有这题做法的code,能不能分享一下,非常感谢!
avatar
h*d
2
没有人知道吗?
avatar
j*t
3
这是trie吗? 应该是一个recursive search吧.
avatar
s*y
4
static final int phoneLength=7;
char [][] keypad={{'0'},{'1'},{'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'}};
void printWords(int [] phoneNumber){
char result []= new char [phoneLength];
doPrintWords(phoneNumber,result,0);
}
void doPrintWords(int []phoneNumber, char []result, int curIndex){
if(curIndex==phoneLength){
System.out.println(result);
return;
}
if(phoneNumber[curIndex]==0){
result[curIndex]='0';
doPrintWords(phoneNumber,result, curIndex+1);
}
else if(phoneNumber[curIndex]==1){
result[curIndex]='1';
doPrintWords(phoneNumber,result, curIndex+1);
}
else{
int numOfchar=keypad[phoneNumber[curIndex]].length;
for(int i=0;iresult[curIndex]=keypad[phoneNumber[curIndex]][i];
doPrintWords(phoneNumber,result, curIndex+1);
}
}
avatar
h*d
5
这个是print电话对应的字母吧?

【在 s********y 的大作中提到】
: static final int phoneLength=7;
: char [][] keypad={{'0'},{'1'},{'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'}};
: void printWords(int [] phoneNumber){
: char result []= new char [phoneLength];
: doPrintWords(phoneNumber,result,0);
: }
: void doPrintWords(int []phoneNumber, char []result, int curIndex){
: if(curIndex==phoneLength){

avatar
l*a
6
每个节点保存所有相关人名list.
struct node
{
int number;
struct node* sub[10];
struct list * head;
struct list * tail;
}
struct list
{
char * name;
struct list * next;
}
剩下的就不用了吧?

【在 h**********d 的大作中提到】
: “手机上给了电话号码求人名索引”
: 谁有这题做法的code,能不能分享一下,非常感谢!

avatar
h*d
7
thanx!

【在 l*****a 的大作中提到】
: 每个节点保存所有相关人名list.
: struct node
: {
: int number;
: struct node* sub[10];
: struct list * head;
: struct list * tail;
: }
: struct list
: {

avatar
c*2
8
This is a lookup problem.
To use prefix tree for this: build the tree with 0-9, at the end node, store
person's name.
(This is similar to routing table lookup.)
Can also be done with hash mapping.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。