zoukankan      html  css  js  c++  java
  • leetcode面试准备:Add and Search Word

    leetcode面试准备:Add and Search Word - Data structure design

    1 题目

    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.

    2 思路

    该题是实现trie树的变种。因此我们首先需要定义节点的数据结构。
    TrieNode节点的属性主要包含以下几点:

    • char content:节点字符内容
    • boolean isEnd:节点是否为一个单词结束的标记
    • LinkedList<TrieNode> childNode: 该节点的所有的孩子节点

    图

    void addWord(String word) Trie一样。
    boolean search(String word) 采用dfs 来进行搜索。

    3 代码

    import java.util.LinkedList;
    
    public class WordDictionary {
    	TrieNode root;
    
    	public WordDictionary() {
    		root = new TrieNode();
    	}
    
    	// Adds a word into the data structure.
    	public void addWord(String word) {
    		if (search(word)) {
    			return;
    		}
    
    		int len = word.length();
    		TrieNode cur = root;
    		for (int i = 0; i < len; i++) {
    			char c = word.charAt(i);
    			TrieNode tmp = cur.subNode(c);
    			if (tmp == null) {
    				tmp = new TrieNode(c);
    				cur.childNode.add(tmp);
    				cur = tmp;
    			} else {
    				cur = tmp;
    			}
    		}
    		cur.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 dfs(root, word, 0);
    	}
    
    	private boolean dfs(TrieNode root, String word, int start) {
    		int len = word.length();
    		if (start == len) {
    			if (root.isEnd)
    				return true;
    			else
    				return false;
    		}
    
    		char c = word.charAt(start);
    		if (c != '.') {
    			TrieNode tmp = root.subNode(c);
    			if (tmp == null)
    				return false;
    			else {
    				return dfs(tmp, word, start + 1);
    			}
    		} else { // '.'
    			for (TrieNode next : root.childNode) {
    				boolean found = dfs(next, word, start + 1);
    				if (found) {
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    
    	static class TrieNode {
    		// Initialize your data structure here.
    		char content; // 节点内容
    		boolean isEnd;// 是否为一个单词的结尾
    		LinkedList<TrieNode> childNode;// 该节点所有的孩子节点
    
    		// Initialize your data structure here.
    		public TrieNode() {
    			this.content = 0;
    			this.isEnd = false;
    			this.childNode = new LinkedList<TrieNode>();
    		}
    
    		public TrieNode(char content) {
    			this.content = content;
    			this.isEnd = false;
    			this.childNode = new LinkedList<TrieNode>();
    		}
    
    		public TrieNode subNode(char c) {
    			for (TrieNode tn : childNode) {
    				if (tn.content == c) {
    					return tn;
    				}
    			}
    			return null;
    		}
    	}
    
    	public static void main(String[] args) {
    		WordDictionary wordDictionary = new WordDictionary();
    		wordDictionary.addWord("bad");
    		wordDictionary.addWord("dad");
    		wordDictionary.addWord("add");
    		boolean res = wordDictionary.search(".ad");
    		System.out.println(res);
    	}
    }
    
    // Your WordDictionary object will be instantiated and called as such:
    
    
    

    4 总结

    很不错的问题,估计面试网易有道,360,百度这样的企业会考到。

  • 相关阅读:
    查看MAC系统JRE和JDK版本
    【转】频点CTO张成:基于Cocos2d的MMORPG开发经验
    Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
    常见android手机分辨率(xxhdpi,xhdpi)
    Android市场官方的统计信息
    【转】腾讯分析移动设备屏幕分辨率分析报告-(数据基于2012年12月至2013年1月上半月)
    error “base class has incomplete type”
    Eclipse 各种包说明
    怎么鉴别五帝钱真假
    【jzoj 6276】树(线段树)(扫描线)
  • 原文地址:https://www.cnblogs.com/byrhuangqiang/p/4806664.html
Copyright © 2011-2022 走看看