LeetCode 677. Map Sum Pairs 键值映射(C++/Java)
题目:
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
分析:
实现一个MapSum类,拥有insert和sum两个方法。
对于方法 insert,你将得到一对(字符串,整数)的键值对。字符串表示键,整数表示值。如果键已经存在,那么原来的键值对将被替代成新的键值对。
对于方法 sum,你将得到一个表示前缀的字符串,你需要返回所有以该前缀开头的键的值的总和。
使用一个map用来存储键值对,每当insert一个字符串时,将这个字符串的所有前缀和val保存到另一个map中,同时插入字符串时,如果map中已经有这个这个前缀的话,就需要更新值。
例如原来插入一个apple-3,此时保存前缀的map中保存a-3,ap-3,app-3,appl-3,apple-3。如果再新插入一个ape-2,我们就要将a-3更新成为a-5,ap-3更新为ap-5,同时新加一个ape-2,也就是将原来的值和新的前缀值相加。
程序:
C++
class MapSum {
public:
/** Initialize your data structure here. */
MapSum() { } void insert(string key, int val) {
int diff = val - vals[key];
for(int i = ; i <= key.size(); ++i){
sums[key.substr(, i)] += diff;
}
vals[key] = val;
} int sum(string prefix) {
return sums[prefix];
}
private:
map<string, int> vals;
map<string, int> sums;
};
Java
class MapSum { /** Initialize your data structure here. */
public MapSum() { } public void insert(String key, int val) {
int diff = val - vals.getOrDefault(key, 0);
for(int i = 1; i <= key.length(); ++i){
String s = key.substring(0, i);
sums.put(s, sums.getOrDefault(s, 0) + diff);
}
vals.put(key, val);
} public int sum(String prefix) {
return sums.getOrDefault(prefix, 0);
}
private HashMap<String, Integer> vals = new HashMap<>();
private HashMap<String, Integer> sums = new HashMap<>();
}
LeetCode 677. Map Sum Pairs 键值映射(C++/Java)的更多相关文章
- leetcode 677. Map Sum Pairs
Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...
- LC 677. Map Sum Pairs
Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...
- 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...
- Java实现 LeetCode 677 键值映射(字典树)
677. 键值映射 实现一个 MapSum 类里的两个方法,insert 和 sum. 对于方法 insert,你将得到一对(字符串,整数)的键值对.字符串表示键,整数表示值.如果键已经存在,那么原来 ...
- Map:containsKey、containsValue 获取Map集合的键值的 值
get(Object key) 返回与指定键关联的值: containsKey(Object key) 如果Map包含指定键的隐射,则返回true: containsValue(Object valu ...
- 【Java必修课】通过Value获取Map中的键值Key的四种方法
1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...
- 通过Value获取Map中的键值Key的四种方法
1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...
- [Swift]LeetCode677. 键值映射 | Map Sum Pairs
Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...
- [LeetCode] Map Sum Pairs 映射配对之和
Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...
随机推荐
- 3.24 7.13 Python基础汇总
对象类型 类型名称 示例 简要说明 备注 数字 int,float,complex 1234,3.14,1.3e5,3+4j 数字大小没有限制 十六进制用0x前缀和0-9,a-f表示 字符串 str ...
- 洛谷$P$4137 $Rmq\ Problem / mex$ 主席树
正解:主席树 解题报告: 传送门$QwQ$ 本来以为是道入门无脑板子题,,,然后康了眼数据范围发现并没有我想像的那么简单昂$kk$ 这时候看到$n$的范围不大,显然考虑离散化?但是又感觉似乎布星?因为 ...
- Markdown数学符号
上标 语法: x^2 效果: \(x^2\) 下标 语法: x_i 效果: \(x_i\) 整体 语法: x^{2y} 效果: \(x^{2y}\) 大括号 语法: \{\} 效果: \(\{\}\) ...
- spring cloud微服务快速教程之(三)声明式访问Feign、负载均衡Ribbon
0-前言 eureka实际上已经集成了负载均衡调度框架Ribbon: 我们有了各个微服务了,那怎么来调用他们呢,一种方法是可以使用 RestTemplate(如:String str= restTem ...
- Could not write JSON: Infinite recursion (StackOverflowError);
转自:https://blog.csdn.net/east123321/article/details/80435051 在controller返回数据到统一json转换的时候,出现了json inf ...
- Faster Rcnn训练自己的数据集过程大白话记录
声明:每人都有自己的理解,动手实践才能对细节更加理解! 一.算法理解 此处省略一万字.................. 二.训练及源码理解 首先配置: 在./lib/utils文件下....运行 p ...
- IPython的介绍与使用
1.IPython简介 ipython是一个python的交互式shell,比默认的python shell好用得多,支持变量自动补全,自动缩进,支持bash shell命令,内置了许多很有用的功能和 ...
- Android Gradle脚本从Groovy迁移到Kotlin DSL
Android Gradle从Groovy迁移到Kotlin Android项目用Gradle构建, 其脚本语言之前是Groovy, 目前也提供了Kotlin的支持, 所以可以迁移到Kotlin. 官 ...
- pandas操作mysql从放弃到入门
目录 相关帮助文档 一.如何读取数据库-read_sql 二.如何筛选数据 三.如何连表-merge 四.如何删除一行或一列-drop 五.如何分组统计-groupyby 六.如何排序-sort_va ...
- NTT - 牛客
链接:https://www.nowcoder.com/acm/contest/133/D来源:牛客网 题目描述 Applese打开了m个QQ群,向群友们发出了组队的邀请.作为网红选手,Applese ...