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 ...
随机推荐
- 解决模糊查询问题 element UI 从服务器搜索数据,输入关键字进行查找
做项目是遇见下拉框的形式,后台返回来3万多条,用element UI中的select选择器中的搜索还是会造成页面卡顿和系统崩溃,因此用了它的远程搜索功能,发现还不错,解决了这个问题. 代码1 < ...
- Spring Boot 配置文件中使用变量、使用随机数
参数引用 在application.properties中的各个参数之间可以直接通过是使用placeHolder的方式进行引用,如: book.author=Clark book.name=C++ b ...
- Linux开发环境及应用—《第三周单元测验》《第四周单元测验》
三单元 1.vi处于文本输入状态时,按下下列哪个按键可以返回命令状态?C A.^ B.$ C.Esc D- 2.vi处于命令状态时,按下下列哪组按键可以把正在编辑的内容保存到磁盘上?D A.Ctrl- ...
- 小小知识点(三十八)MPSK和MQAM调制的实现——利用IQ调制
IQ调制的原理 (一)调制基本原理 (二)调制基本原理 利用IQ调制实现MPSK(QPSK 8PSK BPSK)和MQAM(16QAM 64QAM)调制 (一)利用IQ调制实现QPSK调制 ...
- bash的默认组合键
组合键 组合按键 执行结果 Ctrl+C 终止目前的命令 Ctrl+D 输入结束(EOF),例如邮件结束的时候 Ctrl+M 就是Enter啦! Ctrl+S 暂停屏幕输出 Ctrl+Q 恢复屏幕输出 ...
- 【转】小波与小波包、小波包分解与信号重构、小波包能量特征提取 暨 小波包分解后实现按频率大小分布重新排列(Matlab 程序详解)
转:https://blog.csdn.net/cqfdcw/article/details/84995904 小波与小波包.小波包分解与信号重构.小波包能量特征提取 (Matlab 程序详解) ...
- Linux 7.5 SSH服务和SFTP服务分离
SFTP是SSH的一部分,SFTP没有单独的守护进程,它必须使用SSHD守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像是一个服务器程序,而更像是一个客户端程序. ...
- Friday the Thirteenth 黑色星期五 USACO 模拟 超级简单做法
1003: 1.1.3 Friday the Thirteenth 黑色星期五 时间限制: 1 Sec 内存限制: 128 MB提交: 8 解决: 8[提交] [状态] [讨论版] [命题人:外部 ...
- 位运算上的小技巧 - AtCoder
Problem Statement There is an integer sequence of length 2N: A0,A1,…,A2N−1. (Note that the sequence ...
- 替代not in 和 in 的办法
在程序中,我们经常会习惯性的使用in和not in,在访问量比较小的时候是可以的,但是一旦数据量大了,我们就推荐使用not exists或者外连接来代替了.如果要实现一张表有而另外一张表没有的数据时, ...