LeetCode.677 Map Sum Pairs (字典序和dfs结合应用)

本文介绍了一种名为MapSum的数据结构实现,通过Trie树来完成字符串键值对的插入和查询操作。对于插入操作,如果键已存在则更新其值;对于查询操作,则返回所有以指定前缀开头的键值对的值之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

Implement a MapSum class with insert, and sum methods.

For the method insert, you'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value pair will be overridden to the new one.

For the method sum, you'll be given a string representing the prefix, and you need to return the sum of all the pairs' value whose key starts with the prefix.

Example 1:

Input: insert("apple", 3), Output: Null
Input: sum("ap"), Output: 3
Input: insert("app", 2), Output: Null
Input: sum("ap"), Output: 5

分析:

class TrieNode{
    int times;
    Map<Character,TrieNode> content;
    public TrieNode(){
        content=new HashMap<Character,TrieNode>();
    }
}
public class MapSum {
    //给定单词表,及其出现的次数。
    //当出现新的单词时,更新该单词的次数
    //分别返回由前缀组成的单词个数
    //思路:当遍历到前缀的最后一个字符时,dfs遍历其下面的各次数,因为由该前缀组成的次数便是所求结果

    private TrieNode root;
    /** Initialize your data structure here. */
    public MapSum() {
        root=new TrieNode();
    }
    
    public void insert(String key, int val) {
        //插入
        if(key==null||key.length()==0) return ;
        TrieNode node=root;
        TrieNode tempNode=null;
        
        for(int i=0;i<key.length();i++){
            Character c=key.charAt(i);
            tempNode=node.content.get(c);
            if(tempNode==null){
                //新建节点
                tempNode=new TrieNode();
                //放入node中
                node.content.put(c,tempNode);
            }
            //继续向下
            node=tempNode;
        }
        //已经将单词插入完了,赋值次数
        node.times=val;
        
    }
    
    public int sum(String prefix) {
        //统计由该单词产生的单词总数
        if(prefix==null||prefix.length()==0) return 0;
        
        TrieNode node=root;
        TrieNode tempNode=null;
        for(int i=0;i<prefix.length();i++){
            Character c=prefix.charAt(i);
            tempNode=node.content.get(c);
            if(tempNode==null){
                //说明没有该前缀,直接返回0
                return 0;
            }
            node=tempNode;
        }
        //已经遍历到最后一个字符了,由该HashMap进行dfs遍历
        return dfs(node);
    }
    public int dfs(TrieNode root){
        int count=0;
        for(char c:root.content.keySet()){
            TrieNode temp=root.content.get(c);
            if(temp!=null){
                count+=dfs(temp);
            }
        }
        return count+root.times;
    }
}

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

内容概要:本文深入探讨了Kotlin语言在函数式编程跨平台开发方面的特性优势,结合详细的代码案例,展示了Kotlin的核心技巧应用场景。文章首先介绍了高阶函数Lambda表达式的使用,解释了它们如何简化集合操作回调函数处理。接着,详细讲解了Kotlin Multiplatform(KMP)的实现方式,包括共享模块的创建平台特定模块的配置,展示了如何通过共享业务逻辑代码提高开发效率。最后,文章总结了Kotlin在Android开发、跨平台移动开发、后端开发Web开发中的应用场景,并展望了其未来发展趋势,指出Kotlin将继续在函数式编程跨平台开发领域不断完善发展。; 适合人群:对函数式编程跨平台开发感兴趣的开发者,尤其是有一定编程基础的Kotlin初学者中级开发者。; 使用场景及目标:①理解Kotlin中高阶函数Lambda表达式的使用方法及其在实际开发中的应用场景;②掌握Kotlin Multiplatform的实现方式,能够在多个平台上共享业务逻辑代码,提高开发效率;③了解Kotlin在不同开发领域的应用场景,为选择合适的技术栈提供参考。; 其他说明:本文不仅提供了理论知识,还结合了大量代码案例,帮助读者更好地理解实践Kotlin的函数式编程特性跨平台开发能力。建议读者在学习过程中动手实践代码案例,以加深理解掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值