• LeetCode-208. Implement Trie (Prefix Tree) [C++][Java]


    LeetCode-208. Implement Trie (Prefix Tree)Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/implement-trie-prefix-tree/

    trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

    Implement the Trie class:

    • Trie() Initializes the trie object.
    • void insert(String word) Inserts the string word into the trie.
    • boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
    • boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.

    Example 1:

    Input
    ["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
    [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
    Output
    [null, null, true, false, true, null, true]
    
    Explanation
    Trie trie = new Trie();
    trie.insert("apple");
    trie.search("apple");   // return True
    trie.search("app");     // return False
    trie.startsWith("app"); // return True
    trie.insert("app");
    trie.search("app");     // return True
    

    Constraints:

    • 1 <= word.length, prefix.length <= 2000
    • word and prefix consist only of lowercase English letters.
    • At most 3 * 10^4 calls in total will be made to insertsearch, and startsWith.

    【C++】

    1. class TrieNode {
    2. public:
    3. TrieNode* childNode[26];
    4. bool isVal;
    5. TrieNode(): isVal(false) {
    6. for (int i = 0; i < 26; ++i) {
    7. childNode[i] = nullptr;
    8. }
    9. }
    10. };
    11. class Trie {
    12. TrieNode* root;
    13. public:
    14. Trie(): root(new TrieNode()) {}
    15. void insert(string word) {
    16. TrieNode* temp = root;
    17. for (int i = 0; i < word.size(); ++i) {
    18. if (!temp->childNode[word[i] - 'a']) {
    19. temp->childNode[word[i] - 'a'] = new TrieNode();
    20. }
    21. temp = temp->childNode[word[i] - 'a'];
    22. }
    23. temp->isVal = true;
    24. }
    25. bool search(string word) {
    26. TrieNode* temp = root;
    27. for (int i = 0; i < word.size(); ++i) {
    28. if (!temp) {break;}
    29. temp = temp->childNode[word[i] - 'a'];
    30. }
    31. return temp ? temp->isVal: false;
    32. }
    33. bool startsWith(string prefix) {
    34. TrieNode* temp = root;
    35. for (int i = 0; i < prefix.size(); ++i) {
    36. if (!temp) { break;}
    37. temp = temp->childNode[prefix[i] - 'a'];
    38. }
    39. return temp;
    40. }
    41. };
    42. /**
    43. * Your Trie object will be instantiated and called as such:
    44. * Trie* obj = new Trie();
    45. * obj->insert(word);
    46. * bool param_2 = obj->search(word);
    47. * bool param_3 = obj->startsWith(prefix);
    48. */

    【Java】

    1. class Trie {
    2. private TrieNode root;
    3. private class TrieNode {
    4. public TrieNode[] mChildNode = new TrieNode[26];
    5. public boolean mIsVal = false;
    6. public TrieNode() {
    7. for (int i = 0; i < 26; ++i) {
    8. mChildNode[i] = null;
    9. }
    10. }
    11. }
    12. public Trie() {
    13. root = new TrieNode();
    14. }
    15. public void insert(String word) {
    16. TrieNode temp = root;
    17. for (int i = 0; i < word.length(); ++i) {
    18. if (temp.mChildNode[word.charAt(i) - 'a'] == null) {
    19. temp.mChildNode[word.charAt(i) - 'a'] = new TrieNode();
    20. }
    21. temp = temp.mChildNode[word.charAt(i) - 'a'];
    22. }
    23. temp.mIsVal = true;
    24. }
    25. public boolean search(String word) {
    26. TrieNode temp = root;
    27. for (int i = 0; i < word.length(); ++i) {
    28. if (temp == null) {break;}
    29. temp = temp.mChildNode[word.charAt(i) - 'a'];
    30. }
    31. return temp != null ? temp.mIsVal : false;
    32. }
    33. public boolean startsWith(String prefix) {
    34. TrieNode temp = root;
    35. for (int i = 0; i < prefix.length(); ++i) {
    36. if (temp == null) {break;}
    37. temp = temp.mChildNode[prefix.charAt(i) - 'a'];
    38. }
    39. return temp != null;
    40. }
    41. }
    42. /**
    43. * Your Trie object will be instantiated and called as such:
    44. * Trie obj = new Trie();
    45. * obj.insert(word);
    46. * boolean param_2 = obj.search(word);
    47. * boolean param_3 = obj.startsWith(prefix);
    48. */

  • 相关阅读:
    【Linux】JumpServer 堡垒机远程访问
    MySQL-索引优化和查询优化
    抢购狂欢:跨境电商的区域购物节
    答疑篇-1
    怎么压缩word文档的大小?
    上采样方式(反卷积、插值、反池化)
    查找mac硬盘序列号的方法
    从零开始的Socket编程 一
    J2EE项目部署与发布(Windows版本)
    从零开始学数据结构系列之第四章《 最小生成树概念》
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126328615