当前位置: 首页 > 面试题库 >

尝试执行

左丘成天
2023-03-14
问题内容

我试图在Java中实现一个非常简单的Trie,该Trie支持3种操作。我希望它具有一个insert方法,一个has方法(即trie中的某个单词)和一个toString方法以字符串形式返回trie。我相信我的插入工作正常,但是has和toString证明很困难。到目前为止,这就是我所拥有的。

特里类。

public class CaseInsensitiveTrie implements SimpleTrie {

    //root node
    private TrieNode r;

    public CaseInsensitiveTrie() {
        r = new TrieNode();
    }

    public boolean has(String word) throws InvalidArgumentUosException {
        return r.has(word);
    }

    public void insert(String word) throws InvalidArgumentUosException {
        r.insert(word);
    }

    public String toString() {
        return r.toString();
    }

    public static void main(String[] args) {

        CaseInsensitiveTrie t = new CaseInsensitiveTrie();

        System.out.println("Testing some strings");
        t.insert("TEST");
        t.insert("TATTER");
        System.out.println(t.has("TEST"));
    }
}

和节点类

public class TrieNode {

    //make child nodes
    private TrieNode[] c;
    //flag for end of word
    private boolean flag = false;

    public TrieNode() {
        c = new TrieNode[26]; //1 for each letter in alphabet
    }

    protected void insert(String word) {
        int val = word.charAt(0) - 64;

        //if the value of the child node at val is null, make a new node
                //there to represent the letter
        if (c[val] == null) {
            c[val] = new TrieNode();
        }

        //if word length > 1, then word is not finished being added.
        //otherwise, set the flag to true so we know a word ends there.
        if (word.length() > 1) {
            c[val].insert(word.substring(1));
        } else {
            c[val].flag = true;
        }
    }

    public boolean has(String word) {
        int val = word.charAt(0) - 64;
        if (c[val]!=null && word.length()>1) {
            c[val].has(word.substring(1));
        } else if (c[val].flag==true && word.length()==1) {
            return true;
        }

        return false;
    }

    public String toString() { 
        return "";
    }
}

因此,基本上,在创建Trie时,将创建一个TrieNode作为具有26个子级的根。尝试插入时,将在该根节点上调用insert,该根节点将在正确的位置递归创建一个新节点,并一直持续到单词完成为止。我相信该方法可以正常工作。

我有功能非常坏了,因为我
到有return语句括号出于某种原因之外。我不能将其包含在else子句中,否则编译器会抱怨。除此之外,我认为该方法应该进行一些调整,但我无法终生解决。

toString是我尝试解决的野兽,但是我扔给它的任何东西都没有用,所以我将其保留下来,直到解决问题。如果我有工作,我也许可以找到一种将其重新格式化为toString函数的方法。

int val的用途= word.charAt(0)-64;
是因为输入的每个字符串都必须全部大写(之后将创建一个字符串格式化函数以确保做到这一点),因此第一个字母的int值-64将是其在数组中的位置。也就是说,数组索引0为A,因此A
= 64,A-64 =0。B= 65,B-64 = 1,依此类推。


问题答案:

您的has函数应该看起来像这样:

if (c[val]!=null && word.length()>1) {
    return c[val].has(word.substring(1)); //<-- Change is on this line
} else if (c[val].flag==true && word.length()==1) {
    ...etc

您执行递归调用,但是您确实需要让该值传播回原始调用者。



 类似资料:
  • 问题内容: 当try块中存在时,我对try- finally执行感到困惑。据我了解,finally块将始终执行,即在返回调用方法之前。在考虑以下简单代码时: 实际打印的结果为1。这是否意味着不执行finally块?有人可以帮我吗? 问题答案: 从块返回时,返回值存储在该方法的堆栈帧中。之后,将执行finally块。 更改finally块中的值不会更改堆栈中已存在的值。但是,如果您从finally块

  • 问题内容: 当我尝试在代码中执行HQL时,发生以下异常。我在各个站点中进行了检查,发现antlr.2.7.6.jar shd位于类路径中。我在项目中检查了此内容,发现在我的Maven依赖项中存在该内容。因此,不应有任何此类问题。但是我仍然遇到这个问题。谁能在这方面帮助我。我在“ empList = getHibernateTemplate()。find(“来自Employee”);“行中收到此错误

  • 问题内容: 我正在尝试使用Java中的PreparedStatement执行查询。 尝试执行查询时出现错误号1064(语法错误)。 我已经在MySQL查询浏览器中使用替代值测试了此方法,效果很好。 我的代码有什么问题? 以下是相关代码: 这是我得到的例外: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法有错误;检查与您

  • 问题内容: 在我正在处理的项目中,我需要执行资源文件夹(位于类路径中)中的脚本。我只是在测试最终的脚本功能,因为我在Windows上,所以我需要一种将文件输出到STDIN的方法,因此我创建了一个简单的cat.jar程序来克隆unixs cat命令。 因此,当我执行“ java -jar cat.jar someFile.txt”时,它将输出文件到stdout。我敢肯定,我做的事情有不同的方式。 无

  • 我正在使用org.codehaus.mojo exec-maven-plugin通过我的pom.xml项目执行一个可执行的。jar文件。目前这不起作用,我不确定为什么。当我指定CommandLineArgs时会发生错误。 我的插件如下所示: