[leetcode刷题笔记]Implement Trie (Prefix Tree)

题目链接

一A,开森~

ac代码:

 1 class TrieNode {
 2     // Initialize your data structure here.
 3     char content;
 4     boolean isWord;
 5     int count;
 6     LinkedList<TrieNode> childList;
 7
 8     public TrieNode(char c) {
 9         content = c;
10         isWord = false;
11         count = 0;
12         childList = new LinkedList<TrieNode>();
13     }
14     public TrieNode() {
15         content = ‘ ‘;
16         isWord = false;
17         count = 0;
18         childList = new LinkedList<TrieNode>();
19     }
20
21     public TrieNode findChildC(char c){
22         if(childList != null){
23             for(TrieNode eachChild:childList){
24                 if(eachChild.content == c)
25                     return eachChild;
26             }
27         }
28         return null;
29     }
30 }
31
32 public class Trie {
33     private TrieNode root;
34
35     public Trie() {
36         root = new TrieNode();
37
38     }
39
40     // Inserts a word into the trie.
41     public void insert(String word) {
42         //if already has this word
43         if(search(word) == true)
44             return;
45
46         TrieNode current = root;
47         for(int i = 0;i < word.length();i++){
48             TrieNode child = current.findChildC(word.charAt(i));
49             if(child == null){
50                 TrieNode temp = new TrieNode(word.charAt(i));
51                 current.childList.add(temp);
52                 current = temp;
53             }else{
54                 current = child;
55             }
56             current.count++;
57         }
58         current.isWord = true;
59     }
60
61     // Returns if the word is in the trie.
62     public boolean search(String word) {
63         TrieNode current = root;
64         for(int i = 0;i < word.length();i++){
65             TrieNode child = current.findChildC(word.charAt(i));
66             if(child == null)
67                 return false;
68             else{
69                 current = child;
70                 continue;
71             }
72         }
73         if(current.isWord)
74             return true;
75         return false;
76     }
77
78     // Returns if there is any word in the trie
79     // that starts with the given prefix.
80     public boolean startsWith(String prefix) {
81         TrieNode current = root;
82         for(int i = 0;i < prefix.length();i++){
83             TrieNode child = current.findChildC(prefix.charAt(i));
84             if(child == null)
85                 return false;
86             else{
87                 current = child;
88                 continue;
89             }
90         }
91         return true;
92     }
93 }
94
95 // Your Trie object will be instantiated and called as such:
96 // Trie trie = new Trie();
97 // trie.insert("somestring");
98 // trie.search("key");
时间: 2024-08-03 03:12:18

[leetcode刷题笔记]Implement Trie (Prefix Tree)的相关文章

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模板,每次挨个查看是否所有的串里面是否第i位上都和strs[0]一样,如果都一样,把i位置上的字符放到answer里面,i++,继续循环,否则返回当前的answer. 代码如下: 1 public class Solution { 2 public String longestCommonPrefix

【leetcode刷题笔记】Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 题解:需要找到二叉搜索树中乱序的两个节点,并把它们交换回来

【leetcode刷题笔记】Pow(x, n)

Implement pow(x, n). 题解:注意两点: 普通的递归把n降为n-1会超时,要用二分的方法,每次把xn = x[n/2] * x[n/2] * xn-[n/2]*2, [n/2]表示n除以2下取整. n有可能取负数,负数的时候,先计算pow(x,-n),然后返回1/pow(x,-n); 代码如下: 1 public class Solution { 2 public double pow(double x, int n) { 3 if(n == 0) 4 return 1; 5

【leetcode刷题笔记】Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

【leetcode刷题笔记】Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

【leetcode刷题笔记】String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode刷题笔记】Sqrt(x)

Implement int sqrt(int x). Compute and return the square root of x. 题解:二分的方法,从0,1,2.....x搜索sqrt(x)的值. 代码如下: 1 public class Solution { 2 public int sqrt(int x) { 3 long l = 0; 4 long r = x; 5 6 while(l <= r){ 7 long mid = l + (r-l)/2; 8 if(x == mid*mi

【leetcode刷题笔记】LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(