{"id":965,"date":"2025-12-14T23:12:00","date_gmt":"2025-12-14T15:12:00","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=965"},"modified":"2025-12-14T23:12:00","modified_gmt":"2025-12-14T15:12:00","slug":"leetcode-208-%e5%ae%9e%e7%8e%b0-trie-%e5%89%8d%e7%bc%80%e6%a0%91","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=965","title":{"rendered":"LeetCode 208 &#8211; \u5b9e\u73b0 Trie (\u524d\u7f00\u6811)"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>Trie\uff08\u53d1\u97f3\u7c7b\u4f3c &#8220;try&#8221;\uff09\u6216\u8005\u8bf4 \u524d\u7f00\u6811 \u662f\u4e00\u79cd\u6811\u5f62\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u68c0\u7d22\u5b57\u7b26\u4e32\u6570\u636e\u96c6\u4e2d\u7684\u952e\u3002\u8fd9\u4e00\u6570\u636e\u7ed3\u6784\u6709\u76f8\u5f53\u591a\u7684\u5e94\u7528\u60c5\u666f\uff0c\u4f8b\u5982\u81ea\u52a8\u8865\u5168\u548c\u62fc\u5199\u68c0\u67e5\u3002<\/p>\n<p>\u8bf7\u4f60\u5b9e\u73b0 Trie \u7c7b\uff1a<\/p>\n<p>Trie() \u521d\u59cb\u5316\u524d\u7f00\u6811\u5bf9\u8c61\u3002<br \/>\nvoid insert(String word) \u5411\u524d\u7f00\u6811\u4e2d\u63d2\u5165\u5b57\u7b26\u4e32 word \u3002<br \/>\nboolean search(String word) \u5982\u679c\u5b57\u7b26\u4e32 word \u5728\u524d\u7f00\u6811\u4e2d\uff0c\u8fd4\u56de true\uff08\u5373\uff0c\u5728\u68c0\u7d22\u4e4b\u524d\u5df2\u7ecf\u63d2\u5165\uff09\uff1b\u5426\u5219\uff0c\u8fd4\u56de false \u3002<br \/>\nboolean startsWith(String prefix) \u5982\u679c\u4e4b\u524d\u5df2\u7ecf\u63d2\u5165\u7684\u5b57\u7b26\u4e32 word \u7684\u524d\u7f00\u4e4b\u4e00\u4e3a prefix \uff0c\u8fd4\u56de true \uff1b\u5426\u5219\uff0c\u8fd4\u56de false \u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u524d\u7f00\u6811\u6bcf\u4e2a\u7ed3\u70b9\u662f\u4e00\u4e2a\u5b57\u7b26\uff0c\u7ed3\u70b9\u5185\u6709\u7ed3\u675f\u6807\u8bb0\uff08\u8868\u793a\u6709\u5355\u8bcd\u5728\u8fd9\u91cc\u7ed3\u675f\uff09\uff0c\u540c\u65f6\u53ef\u4ee5\u6307\u5411\u540e\u7eed\u5176\u4ed6\u5b57\u7b26\u3002\u590d\u7528\u7684\u662f\u4ece\u6839\u7ed3\u70b9\u51fa\u53d1\u7684\u8def\u5f84\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u3002<br \/>\n\u4ece\u6839\u7ed3\u70b9\u51fa\u53d1\uff0c\u5b57\u7b26\u4e32\u589e\u52a0\u548c\u67e5\u627e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(\u5b57\u7b26\u4e32\u957f\u5ea6\uff09\u3002<br \/>\n\u4f8b\u5982\uff1a<br \/>\n1. \u63d2\u5165hello\uff1aroot \u2192 h \u2192 e \u2192 l \u2192 l \u2192 o (\u7ed3\u675f)<br \/>\n2. \u63d2\u5165help\uff1aroot \u2192 h \u2192 e \u2192 l \u662f\u5171\u4eab\u7684\uff0c\u5728 l \u4e4b\u540e\u5206\u53c9\u51fa\u65b0\u5206\u652f p(\u7ed3\u675f)<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public class Trie {\n\n    private static class Node {\n\n        public char ch;\n        public boolean isEnd = false;\n        private HashMap&lt;Character, Node&gt; next = new HashMap&lt;&gt;();\n\n        public Node(char ch) {\n            this.ch = ch;\n        }\n\n        private Node findNext(char ch) {\n            return next.get(ch);\n        }\n\n        private Node addNext(char ch) {\n            if (next.containsKey(ch)) {\n                return next.get(ch);\n            }\n            Node node = new Node(ch);\n            next.put(ch, node);\n            return node;\n        }\n\n    }\n\n    private Node root = new Node('0');\n\n    public Trie() {\n\n    }\n\n    public void insert(String word) {\n        Node cur = root;\n        for (int i = 0; i &lt; word.length(); i++) {\n            cur = cur.addNext(word.charAt(i));\n        }\n        cur.isEnd = true;\n    }\n\n    public boolean search(String word) {\n        Node cur = root;\n        for (int i = 0; i &lt; word.length(); i++) {\n            cur = cur.findNext(word.charAt(i));\n            if (cur == null) {\n                return false;\n            }\n        }\n        return cur.isEnd;\n    }\n\n    public boolean startsWith(String prefix) {\n        Node cur = root;\n        for (int i = 0; i &lt; prefix.length(); i++) {\n            cur = cur.findNext(prefix.charAt(i));\n            if (cur == null) {\n                return false;\n            }\n        }\n        return true;\n    }\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">class Trie {\n\n    private class Node(val ch: Char, var isEnd: Boolean = false) {\n        val next = HashMap&lt;Char, Node&gt;()\n\n        fun addNext(ch: Char): Node {\n            return findNext(ch) ?: Node(ch).also {\n                next[ch] = it\n            }\n        }\n\n        fun findNext(ch: Char): Node? {\n            return next[ch]\n        }\n    }\n\n    private val root = Node('0')\n\n\n    fun insert(word: String) {\n        var cur = root\n        for (ch in word) {\n            cur = cur.addNext(ch)\n        }\n        cur.isEnd = true\n    }\n\n    fun search(word: String): Boolean {\n        var cur = root\n        for (ch in word) {\n            cur = cur.findNext(ch) ?: return false\n        }\n        return cur.isEnd\n    }\n\n    fun startsWith(prefix: String): Boolean {\n        var cur = root\n        for (ch in prefix) {\n            cur = cur.findNext(ch) ?: return false\n        }\n        return true\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 Trie\uff08\u53d1\u97f3\u7c7b\u4f3c &#8220;try&#8221;\uff09\u6216\u8005\u8bf4 \u524d\u7f00\u6811 \u662f\u4e00\u79cd\u6811\u5f62\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u9ad8\u6548 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,74,89,45],"class_list":["post-965","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-74","tag-89","tag-45"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=965"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/965\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/965\/revisions\/966"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}