Trie——677. 键值映射、211. 添加与搜索单词 - 数据结构设计

版权声明:版权声明:本文为博主原创文章,转载时请注明出处——作者:冰怜 https://blog.csdn.net/xiaoxin0630/article/details/87820120

677. 键值映射

实现一个 MapSum 类里的两个方法,insert 和 sum

对于方法 insert,你将得到一对(字符串,整数)的键值对。字符串表示键,整数表示值。如果键已经存在,那么原来的键值对将被替代成新的键值对。

对于方法 sum,你将得到一个表示前缀的字符串,你需要返回所有以该前缀开头的键的值的总和。

示例 1:

输入: insert("apple", 3), 输出: Null
输入: sum("ap"), 输出: 3
输入: insert("app", 2), 输出: Null
输入: sum("ap"), 输出: 5

 



import java.util.TreeMap;

class MapSum {

	private class Node{
		
		public int value;
		public TreeMap<Character, Node> next;
		
		public Node(int value){
			this.value=value;
			next=new TreeMap<>();
		}
		
		public Node(){
			this(0);
		}
	}
	
	private Node root;
	
    /** Initialize your data structure here. */
    public MapSum() {
        root=new Node();
    }
    
    /**
     * 插入的字符和值
     * @param word 键
     * @param val 值
     */
    public void insert(String word, int val) {
        
    	Node cur=root;
    	for(int i=0;i<word.length();i++){
    		
    		char c=word.charAt(i);//将word字符串 一个个拆分一个char
    		if(cur.next.get(c) == null)
    			cur.next.put(c, new Node());
    		cur=cur.next.get(c);
    	}
    	cur.value=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.get(c) == null)
    			return 0;
    		cur=cur.next.get(c);
    	}
    	
    	return sum(cur);
    			
    }
    
    private int sum(Node node){
    	
    	//可要可无
//    	if(node.next.size() ==0)//判断是否有元素
//    		return node.value;
    	
    	int res=node.value;
    	for(char c:node.next.keySet())
    		res +=sum(node.next.get(c));
    	
    	return res;
    }
}

/**
 * Your MapSum object will be instantiated and called as such:
 * MapSum obj = new MapSum();
 * obj.insert(key,val);
 * int param_2 = obj.sum(prefix);
 */

211. 添加与搜索单词 - 数据结构设计



import java.util.TreeMap;

class WordDictionary {

	
	private class Node{
		
		public boolean isWord;
		public TreeMap<Character, Node> next;
		
		public Node(boolean isWord){
			this.isWord=isWord;
			next=new TreeMap<>();
		}
		
		public Node(){
			this(false);
		}
		
	}
	
	private Node root;
	
	
    /** Initialize your data structure here. */
    public WordDictionary() {
        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.get(c) == null)
    			cur.next.put(c, new Node());
    		cur=cur.next.get(c);
    	}
    	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 match(root, word, 0);
    }
    
    /**
     * "."匹配任何字符 正则表达式
     * @param node 整体Trie树  获取内容进行匹配
     * @param word 搜索的内容
     * @param index 搜索的位置 默认从根节点开始 往下递归就+1
     * @return
     */
    private boolean match(Node node,String word,int index){
    	
    	if(index == word.length())//到底了 返回结果
    		return node.isWord;
    	
    	char c=word.charAt(index);
    	if(c !='.'){//不为‘.’
    		if(node.next.get(c) ==null)//判断内容是不是为空 如果为空就返回false
    			return false;
    		return match(node.next.get(c), word, index+1);
    	}else{//如果为'.',循环继续递归下去
    		for(char nextChar : node.next.keySet())
    			if(match(node.next.get(nextChar), word, index+1))//如果匹配成功 返回true
    				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);
 */

猜你喜欢

转载自blog.csdn.net/xiaoxin0630/article/details/87820120
今日推荐