- 浏览: 182735 次
- 性别:
- 来自: 济南
文章分类
最新评论
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
设计一个字典树(前缀树),题目中假设字典中包含的字母为a-z, 我们可以设定树中每个节点都有两个属性,一个为TrieNode[], 另一个为一个布尔变量isLast。TrieNode[] 用来存放insert操作时对应的字符,isLast对应search操作是是否包含这个元素。代码如下:
Note:
You may assume that all inputs are consist of lowercase letters a-z.
设计一个字典树(前缀树),题目中假设字典中包含的字母为a-z, 我们可以设定树中每个节点都有两个属性,一个为TrieNode[], 另一个为一个布尔变量isLast。TrieNode[] 用来存放insert操作时对应的字符,isLast对应search操作是是否包含这个元素。代码如下:
class TrieNode { boolean isLast; TrieNode[] trieNode; // Initialize your data structure here. public TrieNode() { isLast = false; trieNode = new TrieNode[26]; } } public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } // Inserts a word into the trie. public void insert(String word) { TrieNode cur = root; for(char c : word.toCharArray()) { if(cur.trieNode[c - 'a'] == null) { cur.trieNode[c - 'a'] = new TrieNode(); } cur = cur.trieNode[c - 'a']; } cur.isLast = true; } // Returns if the word is in the trie. public boolean search(String word) { TrieNode cur = root; for(char c : word.toCharArray()) { if(cur.trieNode[c - 'a'] == null) return false; cur = cur.trieNode[c - 'a']; } return cur.isLast; } // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String prefix) { TrieNode cur = root; for(char c : prefix.toCharArray()) { if(cur.trieNode[c - 'a'] == null) return false; cur = cur.trieNode[c - 'a']; } return true; } } // Your Trie object will be instantiated and called as such: // Trie trie = new Trie(); // trie.insert("somestring"); // trie.search("key");
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 264Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 266You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 381Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 372Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 560Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 474Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 661Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 468The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 427Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 571Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 576Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 423All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 894Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 928Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 668Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 841Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 780You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 703For a undirected graph with tre ...
相关推荐
python python_leetcode题解之208_Implement_Trie_(Prefix_Tree).py
不会树实现 使用插入、搜索和startsWith 方法实现一个trie。 Example: Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns false trie.starts...
In this project you are supposed to implement a B+ tree of order 3 with the following operations: initialize insert (with splitting) and search. The B+ tree should be able to print out itself.Input ...
树的java实现 , 很好的一个应用, ,同时结合了数据库的操作.是一个不错的学习的实例了.
Java 中 extend 与 implement 的区别 Java 语言中,extend 和 implement 是两个基本概念,它们之间的区别是非常重要的。extend 用于继承类,而 implement 用于实现接口。在 Java 中,不支持多重继承,但是可以使用...
kd树很基本的算法介绍和实现方法概括。... In this assignment, you will implement a special data structure called a kd-tree (short for “k-dimensional tree”) that efficiently supports this opera- tion.
jcifs_java_implement_cifs jcifs_java_implement_cifs jcifs_java_implement_cifs jcifs_java_implement_cifs jcifs_java_implement_cifs jcifs_java_implement_cifs
While most C programmers use APIs and the libraries that implement them in almost every application they write, relatively few programmers create and disseminate new, widely applicable APIs....
Eclipse Implementor插件是专为Eclipse IDE设计的一款实用工具,它主要目的是为了帮助开发者更高效地实现接口或抽象类的方法。在Java编程中,当我们需要为一个接口或抽象类提供具体实现时,手动编写这些方法可能会...
7.4.1 Packet Tracer - Implement DHCPv4 Cisco Packet Tracer 思科模拟器 正确答案文件 由于程序问题无法保存激活DHCP端口配置 另加满分步骤截图 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次...
6.4.1 Packet Tracer - Implement Etherchannel Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...
1.6.1 Packet Tracer - Implement a Small Network Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...
3.6.1 Packet Tracer - Implement VLANs and Trunking Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...
C++ 针对 文件 操作 英文习题 Read the content of a file, and output to the screen. Every time after output 10 lines, ask users if they want to stop the program.
非完美pdf版本,但是可以看 非完美pdf版本,但是可以看
By the end of this book, you should be ready to implement advanced, effective and efficient CNN models at your professional project or personal initiatives by working on complex image and video ...
5. **Implement Trie (Prefix Tree)** (Medium): 实现一个前缀树(字典树)数据结构。主要用于高效地进行字符串查询和插入,适合存储单词。 6. **Add and Search Word - Data structure design** (Medium): 在一个...
BI Design and implementation, BI Design and implementation, BI Design and implementation, BI Design and implementation,
There are also two base clases VirtualizingItemsControl and VirtualizingItemContainer which can be used to implement your own virtualizing items control. Main advantage of VirtualizingTreeView over...
为了提高性能并减少内存使用,该程序使用Double Array Trie而不是常用的Linked List Trie 。 在基准测试中, it is 10 times faster than the most popular AC algorithm implement in golang @ github and tenth ...