有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java搜索建议系统

下面是问题陈述,下面是我使用三节点数据结构的解决方案,但我的ans是错误的,我无法理解我的代码哪里出错了,请帮助

给定一个字符串数组和一个字符串搜索词。我们希望设计一个系统,在输入searchWord的每个字符后,从产品中最多建议三个产品名称。建议的产品应具有与searchWord相同的前缀。如果有三个以上的产品具有公共前缀,则返回三个字典最小产品

键入searchWord的每个字符后返回建议产品列表

例1:

Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"<br>
Output: [["mobile","moneypot","monitor"],
      ["mobile","moneypot","monitor"],
     ["mouse","mousepad"],
     ["mouse","mousepad"],
     ["mouse","mousepad"]]

说明:产品按字典顺序排序=[“手机”、“钱罐”、“显示器”、“鼠标”、“鼠标垫”] 在输入m和mo后,所有产品都匹配,我们向用户显示[“移动”、“moneypot”、“monitor”] 输入mou、mou和鼠标后,系统提示[“鼠标”、“鼠标垫”]

import java.util.*;
class Solution {
class TrieNode{
    TrieNode child[];
    boolean isTerminating;
    String word;
    char ch;
    TrieNode(){
        this.child=new TrieNode[26];
        this.isTerminating=false;
        this.word="";
    }
}
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
    TrieNode root=new TrieNode();
    add(root,products);
    return search(root,searchWord);
}
public void add(TrieNode root,String product[]){
    for(int i=0;i<product.length;i++)
        addWord(root,product[i],product[i]);
}
public void addWord(TrieNode root,String word,String product){
    char ch=word.charAt(0);
    int index=ch-'a';
    if(word.length() == 1){
        TrieNode node;
        if(root.child[index] == null){
            node=new TrieNode();
            node.isTerminating=true;
            node.word=product;
            root.child[index]=node;
        }
        else{
            node=new TrieNode();
            node.isTerminating=true;
            node.word=product;
        }
        return;
    }
    if(root.child[index] == null){
        TrieNode node=new TrieNode();
        root.child[index]=node;
        addWord(root,word.substring(1),product);
    }
    else
        addWord(root.child[index],word.substring(1),product);
}

//correctly working
public List<List<String>> search(TrieNode root,String word){
    List<List<String>> list=new ArrayList<>();
    for(int i=0;i<word.length();i++){
        String str=word.substring(0,i+1);
        List<String> l=searchWord(root,str);
        list.add(l);
    }
    return list;
}
public List<String> searchWord(TrieNode root,String str){
    char ch=str.charAt(0);
    int index=ch-'a';
    if(str.length() == 1){
        if(root.child[index] == null)
            return new ArrayList<>();
        List<String> l=new ArrayList<>();
        addToList(l,root);
        print(l,str);
        return sort(l);
    }
    if(root.child[index] == null)
        return new ArrayList<>();
    return searchWord(root.child[index],str.substring(1));
}
public void print(List<String> l,String str){
    System.out.print(str+" : ");
    for(int i=0;i<l.size();i++)
        System.out.print(l.get(i)+" ");
    System.out.println();
}
public void addToList(List<String> l , TrieNode root){
    if(root == null)
        return;
    for(int i=0;i<26;i++){
        if(root.child[i] != null){
            TrieNode node=root.child[i];
            if(node.isTerminating){
                l.add(node.word);
            }
            addToList(l,node);
        }
    }
}

// lexicographically sorted array

public List<String> sort(List<String> l){
    int length=l.size();
    String words[]=new String[l.size()];
    for(int i=0;i<length;i++){
        words[i]=l.get(i);
    }
    Arrays.sort(words);
    List<String> list=new ArrayList<>();
    if(length > 3){
        list.add(words[0]);
        list.add(words[1]);
        list.add(words[2]);
        return list;
    }
    for(int i=0;i<length;i++)
        list.add(words[i]);
    return list;
     }
}

共 (1) 个答案

  1. # 1 楼答案

    如果我不得不猜测,除了解决方案的性能之外,功能问题可能是:

    public List<String> searchWord(TrieNode root,String str){
        char ch=str.charAt(0);
        int index=ch-'a';
        if(str.length() == 1){
            if(root.child[index] == null)
                return new ArrayList<>();
            List<String> l=new ArrayList<>();
      
            addToList(l,root); // <<<<<<<<<<<<<<<<< THIS LINE
    
            print(l,str);
            return sort(l);
        }
        if(root.child[index] == null)
            return new ArrayList<>();
        return searchWord(root.child[index],str.substring(1));
    }
    

    函数将节点下的所有终端添加到列表中。你用root来称呼它,但你可能想用root.child[index]来称呼它

    考虑输入的“谅解备忘录”。{}将是表示前缀mo的节点,字符串将是"u"。您将返回一个包含“mobile”的列表,该列表与“mou”输入不匹配


    至于性能,只需对列表排序,对前缀进行二进制搜索,如果前缀匹配,则返回接下来的三个索引。不需要trie(尽管trie作为排序数组的索引会有更好的性能),也不需要歌舞来对列表进行排序,只会丢弃除三个值以外的所有值