C++实现LeetCode(211.添加和查找单词-数据结构设计)

篇文章主要介绍了C++实现LeetCode(211.添加和查找单词-数据结构设计),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

[LeetCode] 211.Add and Search Word - Data structure design 添加和查找单词-数据结构设计

Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.

click to show hint.

You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.

LeetCode出新题的速度越来越快了,有点跟不上节奏的感觉了。这道题如果做过之前的那道 Implement Trie (Prefix Tree) 实现字典树(前缀树)的话就没有太大的难度了,还是要用到字典树的结构,唯一不同的地方就是search的函数需要重新写一下,因为这道题里面'.'可以代替任意字符,所以一旦有了'.',就需要查找所有的子树,只要有一个返回true,整个search函数就返回true,典型的DFS的问题,其他部分跟上一道实现字典树没有太大区别,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

class WordDictionary {

public:

    struct TrieNode {

    public:

        TrieNode *child[26];

        bool isWord;

        TrieNode() : isWord(false) {

            for (auto &a : child) a = NULL;

        }

    };

     

    WordDictionary() {

        root = new TrieNode();

    }

     

    // Adds a word into the data structure.

    void addWord(string word) {

        TrieNode *p = root;

        for (auto &a : word) {

            int i = a - 'a';

            if (!p->child[i]) p->child[i] = new TrieNode();

            p = p->child[i];

        }

        p->isWord = true;

    }

    // Returns if the word is in the data structure. A word could

    // contain the dot character '.' to represent any one letter.

    bool search(string word) {

        return searchWord(word, root, 0);

    }

     

    bool searchWord(string &word, TrieNode *p, int i) {

        if (i == word.size()) return p->isWord;

        if (word[i] == '.') {

            for (auto &a : p->child) {

                if (a && searchWord(word, a, i + 1)) return true;

            }

            return false;

        } else {

            return p->child[word[i] - 'a'] && searchWord(word, p->child[word[i] - 'a'], i + 1);

        }

    }

     

private:

    TrieNode *root;

};

// Your WordDictionary object will be instantiated and called as such:

// WordDictionary wordDictionary;

// wordDictionary.addWord("word");

// wordDictionary.search("pattern");

讨论:这道题有个很好的Follow up,就是当搜索的单词中存在www.meimeitu8.com星号怎么搞,星号的定义和Wildcard Matching中一样,可以代表任意的字符串,包括空字符串,请参见评论区1楼。

类似题目:

Implement Trie (Prefix Tree)

Wildcard Matching

参考资料:

https://leetcode.com/discuss/36246/my-java-trie-based-solution

到此这篇关于C++实现LeetCode(211.添加和查找单词-数据结构设计)的文章就介绍到这了,更多相关C++实现添加和查找单词-数据结构设计内容请搜索脚本之家以前的文章或继续浏览下面的相关文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值