211. Add and Search Word - Data structure design

题目链接:https://leetcode.com/problems/add-and-search-word-data-structure-design/

设计一个支持以下两种操作的数据结构:

void addWord(word)
bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。

示例:

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

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

 

思路:

用trie树来解决,不了解trie树的来传送门:传送门

然后题目中唯一的特殊点就是.可以代替任何字母,所匹配到.字符时,对后续的26个孩子节点进行遍历回溯即可。

AC 

Runtime: 79 ms, faster than 81.21% of Java online submissions for Add and Search Word - Data structure design.

Memory Usage: 58.3 MB, less than 56.97% of Java online submissions forAdd and Search Word - Data structure design.

class TrieNode{
    TrieNode[] children;
    boolean isEnd;
    public TrieNode(){
        children =new TrieNode[26];
        isEnd=false;
    }
}
class WordDictionary {
    TrieNode root;
    /** Initialize your data structure here. */
    public WordDictionary() {
        root=new TrieNode();
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TrieNode temp=root;
        for(char c:word.toCharArray()){
            if(temp.children[c-'a']==null){
                temp.children[c-'a']=new TrieNode();
            }
            temp=temp.children[c-'a'];
        }
        temp.isEnd=true;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        return helper(word,root,0);
    }
    public boolean helper(String word,TrieNode temp,int index){
        if(temp==null)
            return false;
        if(index==word.length())
            return temp.isEnd;
        char c=word.charAt(index);
        if(c!='.'){
            if(temp.children[c-'a']==null)
                return false;
            else
                return helper(word,temp.children[c-'a'],index+1);
        }else{
            for(TrieNode tn:temp.children){
                if(helper(word,tn,index+1))
                    return true;
            }
            return false;
        }
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值