• 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. */

  • 相关阅读:
    智慧铁路:机车整备场数字孪生
    2023-mac brew安装python最新版本,遇见的问题和处理方式
    虚拟化与Docker
    【Java八股文总结】之Java设计模式
    V8中的快慢数组(附源码、图文更易理解😃)
    为什么科技型企业需要“贯标”?
    RMAN-08137 主库无法删除归档文件
    SpringMVC---->自我实现底层机制(吃透springMVC)
    构建客户门户的痛点及低代码工具解决方案
    异步过渡方案—Generator
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126328615