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.

题意:

设计一种数据结构支持下面两种操作:

void addWord(word);

bool search(word);

search(word)可以搜索一个单词或者一个只包含a-z或者..A的正则表达式字符串,(.)点字符意思是可以代表任何一个字母字符。

Note:

1、可以假设所有单词都是由a-z之间的小写字母组成。

Hint:

你应该熟悉单词查找树是如何工作的。如果不熟悉,请先解决Implement Trie (Prefix Tree)题目。


转载地址:http://www.cnblogs.com/grandyang/p/4507286.html

思路:

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

代码:C++版:108ms

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");


ntation of a dynamic array data structure in C++ that can be used for reliability and maintainability simulation. Here's an implementation of a Vector class in C++: ```c++ #include <iostream> #include <stdexcept> template <typename T> class Vector { private: T* m_data; size_t m_capacity; size_t m_size; public: Vector() : m_data(nullptr), m_capacity(0), m_size(0) {} Vector(size_t size) : m_data(new T[size]), m_capacity(size), m_size(size) {} Vector(const Vector& other) : m_data(new T[other.m_capacity]), m_capacity(other.m_capacity), m_size(other.m_size) { for (size_t i = 0; i < m_size; ++i) { m_data[i] = other.m_data[i]; } } ~Vector() { delete[] m_data; } Vector& operator=(const Vector& other) { if (this != &other) { delete[] m_data; m_data = new T[other.m_capacity]; m_capacity = other.m_capacity; m_size = other.m_size; for (size_t i = 0; i < m_size; ++i) { m_data[i] = other.m_data[i]; } } return *this; } size_t size() const { return m_size; } size_t capacity() const { return m_capacity; } bool empty() const { return m_size == 0; } void reserve(size_t capacity) { if (capacity > m_capacity) { T* temp = m_data; m_data = new T[capacity]; m_capacity = capacity; for (size_t i = 0; i < m_size; ++i) { m_data[i] = temp[i]; } delete[] temp; } } void resize(size_t size) { reserve(size); m_size = size; } void push_back(const T& value) { if (m_capacity == 0) { reserve(1); } else if (m_size == m_capacity) { reserve(2 * m_capacity); } m_data[m_size++] = value; } void pop_back() { if (empty()) { throw std::out_of_range("Vector is empty"); } --m_size; } T& operator[](size_t index) { if (index >= m_size) { throw std::out_of_range("Index out of range"); } return m_data[index]; } const T& operator[](size_t index) const { if (index >= m_size) { throw std::out_of_range("Index out of range"); } return m_data[index]; } }; ``` This implementation includes methods to get the size, capacity, and emptiness of the vector, as well as methods to reserve space, resize the vector, and add/remove elements at the back. It also provides the [] operator for accessing elements by index.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值