LeetCode Implement Trie (Prefix Tree)

原创 2015年07月09日 10:53:11

Description:

Implement a trie with insertsearch, and startsWith methods.

Solution:

Trie树是字符串常用的一个数据结构,AC自动机也会用到。这里需要注意的是,prefix和search功能的不一样,也就要求了要在每个节点中添加一个标记,记录当前节点是否表示一个字符串的结束。

public class Trie {
	private TrieNode root;

	public Trie() {
		root = new TrieNode();
	}

	// Inserts a word into the trie.
	public void insert(String word) {
		TrieNode temp = root, next;
		int ch;
		for (int i = 0; i < word.length(); i++) {
			ch = word.charAt(i) - 'a';
			next = temp.nextNodes[ch];
			if (next == null) {
				next = new TrieNode();
				temp.nextNodes[ch] = next;
			}
			temp = next;
		}
		temp.isEnd = true;
	}

	// Returns if the word is in the trie.
	public boolean search(String word) {
		TrieNode temp = root, next;
		int ch;

		for (int i = 0; i < word.length(); i++) {
			ch = word.charAt(i) - 'a';
			next = temp.nextNodes[ch];
			if (next == null)
				return false;
			temp = next;
		}

		return temp.isEnd;
	}

	// Returns if there is any word in the trie
	// that starts with the given prefix.
	public boolean startsWith(String prefix) {
		TrieNode temp = root, next;
		int ch;

		for (int i = 0; i < prefix.length(); i++) {
			ch = prefix.charAt(i) - 'a';
			next = temp.nextNodes[ch];
			if (next == null)
				return false;
			temp = next;
		}
		return true;
	}

	public static void main(String[] args) {
		Trie t = new Trie();
		t.insert("abb");
		t.insert("c");
		System.out.println(t.search("a"));
		System.out.println(t.startsWith("a"));
	}
}

class TrieNode {
	TrieNode[] nextNodes = new TrieNode[26];
	boolean isEnd;

	TrieNode() {

	}

}


Leetcode 208: Implement Trie (Prefix Tree)

Implement Trie (Prefix Tree) Total Accepted: 601 Total Submissions: 2396 Implement a tri...
  • xudli
  • xudli
  • 2015年05月09日 06:59
  • 3980

LeetCode 208: Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a...
  • sunao2002002
  • sunao2002002
  • 2015年06月27日 15:32
  • 3213

Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods.Note: You may assume that all inputs a...
  • u010786672
  • u010786672
  • 2015年05月08日 21:32
  • 872

[LeetCode]Implement Trie(Prefix Tree),解题报告

目录目录 概述 Trie树基本实现 定义Trie树节点 添加操作 查询word是否在Trie树中 AC完整代码概述Trie树,又称为字典树、单词查找树或者前缀树,是一种用于快速检索的多叉数结构。例如,...
  • zinss26914
  • zinss26914
  • 2015年05月15日 14:12
  • 4556

[LeetCode 208] Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all in...
  • sbitswc
  • sbitswc
  • 2015年08月24日 13:09
  • 616

LeetCode208:Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods.Note: You may assume that all inputs a...
  • u012501459
  • u012501459
  • 2015年07月03日 15:44
  • 808

LeetCode208 Implement Trie (Prefix Tree) Java

题目: Implement a trie with insert, search, and startsWith methods.Note: You may assume that all in...
  • l294265421
  • l294265421
  • 2016年04月22日 21:59
  • 562

Implement Trie (Prefix Tree) LeetCode Java

Implement a trie with insert, search, and startsWith methods.Note: You may assume that all inputs a...
  • zuoyexingchennn
  • zuoyexingchennn
  • 2015年06月01日 19:50
  • 199

leetcode[208]:Implement Trie (Prefix Tree)

【题目】 Implement a trie with insert, search, and startsWith methods.Note: You may assume that all i...
  • sunyangwei1993
  • sunyangwei1993
  • 2016年08月20日 15:06
  • 287

leetcode:Implement Trie (Prefix Tree)

Implement Trie (Prefix Tree) Total Accepted: 2015 Total Submissions: 8299 My Submissions Question So...
  • chanrenyuan
  • chanrenyuan
  • 2015年05月14日 00:13
  • 213
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Implement Trie (Prefix Tree)
举报原因:
原因补充:

(最多只允许输入30个字)