算法与数据结构-字典树

前言

大四重拾算法与数据结构,所有内容为自己的阶段小结所以并不是技术性文章,如有兴趣阅读,遇到问题不妨给我留个言,万分感谢!
字典树
复杂度为O(l) l为字符串的长度
比较简单直接付码

package Trie;

import java.util.HashMap;

public class Trie {
	private class Node {
		public boolean isWord;//判断是不是一个单词
		public HashMap<Character, Node> next;

		public Node() {
			isWord = false;
			next = new HashMap<>();
		}

		public Node(boolean isWord) {
			this.isWord = isWord;
			next = new HashMap<>();
		}
	}

	private Node root;
	private int size;

	public Trie() {
		root = new Node();
	}

	public int getSize() {
		return size;
	}

	public void add(String word) {
		Node cur = root;
		for (int i = 0; i < word.length(); i++) {
			char c = word.charAt(i);
			if (!cur.next.containsKey(c)) {
				cur.next.put(c, new Node());
			}
			cur = cur.next.get(c);
		}
		if (!cur.isWord) {
			cur.isWord = true;
			size++;
		}
	}

	public boolean contains(String word) {
		Node cur = root;
		for (int i = 0; i < word.length(); i++) {
			char c = word.charAt(i);
			if(!cur.next.containsKey(c)) {
				return false;
			}
			cur = cur.next.get(c);
		}
		return cur.isWord;
	}
}

下面是LeetCode上的题目
211. Add and Search Word - Data structure design
附上解决代码

package Trie;

import java.util.HashMap;

public class LeetCode211 {
	private class Node {
		public boolean isWord;
		public HashMap<Character, Node> next;

		public Node() {
			isWord = false;
			next = new HashMap<>();
		}
	}

	private Node root;

	/** Initialize your data structure here. */
	public LeetCode211() {
		root = new Node();
	}

	/** Adds a word into the data structure. */
	public void addWord(String word) {
		Node cur = root;
		for (int i = 0; i < word.length(); i++) {
			char c = word.charAt(i);
			if (!cur.next.containsKey(c)) {
				cur.next.put(c, new Node());
			}
			cur = cur.next.get(c);
		}
		if (!cur.isWord) {
			cur.isWord = 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 search(root, word, 0);
	}

	private boolean search(Node node, String word, int index) {
		if(index == word.length())
			return node.isWord;
		char c = word.charAt(index);
		//如果不是.的话 正常搜索
		if (c != '.') {
			if (!node.next.containsKey(c)) {
				return false;
			}
			return search(node.next.get(c),word,index + 1);
		} else {
			//如果是点的话遍历接下来的孩子 进行匹配
			for (char nexChar : node.next.keySet()) {
				if(search(node.next.get(nexChar),word,index + 1))
					return true;
			}
			return false;
		}
	}
}

677. Map Sum Pairs

package Trie;

import java.util.HashMap;
/**
 *  	 每一个单词存在一个值  要求给出单词的前缀   计算含有此前缀的单词值的和
 *   	 用递归的方式累加总和  结点的isWord替换成val 添加正常添加
 */
public class LeetCode677 {
	private class Node {
		public int val;
		public HashMap<Character, Node> next;

		public Node() {
			this.val = 0;
			next = new HashMap<>();
		}
	}

	private Node root;

	/** Initialize your data structure here. */
	public LeetCode677() {
		root = new Node();
	}

	public void insert(String key, int val) {
		Node cur = root;
		for (int i = 0; i < key.length(); i++) {
			char c = key.charAt(i);
			if (!cur.next.containsKey(c)) {
				cur.next.put(c, new Node());
			}
			cur = cur.next.get(c);
		}
		cur.val = val;
	}

	public int sum(String prefix) {
		Node cur = root;
		for (int i = 0; i < prefix.length(); i++) {
			char c = prefix.charAt(i);
			if (!cur.next.containsKey(c)) {
				return 0;
			}
			cur = cur.next.get(c);
		}
		return sum(cur);
	}

	private int sum(Node node) {
		int res = node.val;
		for(char c : node.next.keySet()) {
			res += sum(node.next.get(c));
		}
		return res;
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值