leecode 解题总结:211. Add and Search Word - Data structure design

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
/*
问题:
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.

分析:分析"."或"..",代表任意字符,需要先实现前缀树,则对于"."的处理需要发给所有孩子节点去查询
则存在递归调用多个,只要有一个符合就返回true

前缀树可以不用递归,后缀树需要

输入:
7(指令个数)
addWord bad
addWord dad
addWord mad
search pad
search bad
search .ad
search b..

输出:
false
true
true
true

关键:
1 通配符:添加+搜索 是前缀树的结构,"."代表任意字符,需要先实现前缀树,则对于"."的处理需要发给所有孩子节点去查询
则存在递归调用多个,只要有一个符合就返回true
2
		//遇到"."需要递归查找后续所有非空节点
		if('.' == word.at(pos))
		{
			for(int i = 0 ; i < CHILD_SIZE ; i++)
			{
				childNode = _childs[i];
				//如何判断查找结束,当前待查找字符对应节点存在,且完成标记为真
				if(pos == len - 1 && (childNode) && childNode->_isFinish)
				{
					return true;
				}
				if(childNode)
				{
					bool result = childNode->search(word,  pos+1);
					if(result)
					{
						return true;
					}
				}
			}
			return false;
		}
*/

const int CHILD_SIZE = 26;
class TrieNode
{
public:
	TrieNode()
	{
		_isFinish = false;
		memset(_childs , NULL , sizeof(_childs));
	}

	bool search(string& word  ,int pos) {
		if(word.empty() || pos < 0 || pos >= word.length())
		{
			return false;
		}
		int len = word.length();
		TrieNode* childNode = NULL;
		//遇到"."需要递归查找后续所有非空节点
		if('.' == word.at(pos))
		{
			for(int i = 0 ; i < CHILD_SIZE ; i++)
			{
				childNode = _childs[i];
				//如何判断查找结束,当前待查找字符对应节点存在,且完成标记为真
				if(pos == len - 1 && (childNode) && childNode->_isFinish)
				{
					return true;
				}
				if(childNode)
				{
					bool result = childNode->search(word,  pos+1);
					if(result)
					{
						return true;
					}
				}
			}
			return false;
		}
		else
		{
			int index = word.at(pos) - 'a';
			if(index < 0 || index > 25)
			{
				return false;
			}
			childNode = _childs[index];
			//如何判断查找结束,当前待查找字符对应节点存在,且完成标记为真
			if(pos == len - 1 && childNode && childNode->_isFinish)
			{
				return true;
			}
			if(childNode)
			{
				return childNode->search(word , pos+1);
			}
			else
			{
				return false;
			}
		}
	}
public:
	bool _isFinish;
	TrieNode* _childs[CHILD_SIZE];
};

class WordDictionary {
public:
    /** Initialize your data structure here. */
    WordDictionary() {
        _root = new TrieNode();
    }
    
    /** Adds a word into the data structure. */
    void addWord(string word) {
		if(word.empty())
		{
			return ;
		}
		int len = word.length();
		TrieNode* curNode = _root;
		for(int i = 0 ; i < len ; i++)
		{
			int index = word.at(i) - 'a';
			if(index < 0 || index > 25)
			{
				return;
			}
			TrieNode* childNode = curNode->_childs[index];
			if(!childNode)
			{
				childNode = new TrieNode();
				curNode->_childs[index] = childNode;//注意赋值
			}
			curNode = childNode; 
		}     
		curNode->_isFinish = 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) {
		if(word.empty())
		{
			return false;
		}
		bool result = _root->search(word , 0);
		return result;
    }

	~WordDictionary()
	{
		deleteNode(_root);
	}

	void deleteNode(TrieNode* root)
	{
		if(!root)
		{
			return;
		}
		for(int i = 0 ; i < CHILD_SIZE ; i++)
		{
			TrieNode* childNode = root->_childs[i];		
			if(childNode)
			{
				deleteNode(childNode);
			}
		}
		if(root)
		{
			delete root;
			root = NULL;
		}
	}

private:
	TrieNode* _root;
};

void print(vector<int>& result)
{
	if(result.empty())
	{
		cout << "no result" << endl;
		return;
	}
	int size = result.size();
	for(int i = 0 ; i < size ; i++)
	{
		cout << result.at(i) << " " ;
	}
	cout << endl;
}

void process()
{
	 vector<int> nums;
	 string value;
	 int commandNum;
	 vector<int> result;
	 string command;
	 while(cin >> commandNum)
	 {
		 WordDictionary obj;
		for(int i = 0 ; i < commandNum ; i++)
		{
			cin >> command >> value;
			if("addWord" == command)
			{
				obj.addWord(value);
			}
			else if("search" == command)
			{
				bool result = obj.search(value);
				if(result)
				{
					cout << "true" << endl;
				}
				else
				{
					cout << "false" << endl;
				}
			}
		}
		cout << endl;
	 }
}

int main(int argc , char* argv[])
{
	process();
	getchar();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值