题目:

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)的更多相关文章

  1. leetcode 677. Map Sum Pairs

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

  2. LC 677. Map Sum Pairs

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

  3. 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...

  4. Java实现 LeetCode 677 键值映射(字典树)

    677. 键值映射 实现一个 MapSum 类里的两个方法,insert 和 sum. 对于方法 insert,你将得到一对(字符串,整数)的键值对.字符串表示键,整数表示值.如果键已经存在,那么原来 ...

  5. Map:containsKey、containsValue 获取Map集合的键值的 值

    get(Object key) 返回与指定键关联的值: containsKey(Object key) 如果Map包含指定键的隐射,则返回true: containsValue(Object valu ...

  6. 【Java必修课】通过Value获取Map中的键值Key的四种方法

    1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...

  7. 通过Value获取Map中的键值Key的四种方法

    1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...

  8. [Swift]LeetCode677. 键值映射 | Map Sum Pairs

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

  9. [LeetCode] Map Sum Pairs 映射配对之和

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

随机推荐

  1. 小白进阶之路-python格式化输出

    1.不使用格式化前:传值时比较麻烦而且不精确. 2.格式化输出后,%代表先占个位,s代表字符串,%s代表这里之后会传一个字符串类型的值,%s其实可以接受任意类型的值:%d传入的值要求必须是数字. %s ...

  2. $Poj2083/AcWing118\ Fractal$ 模拟

    $AcWing$ $Sol$ 一年前做过差不多的南蛮图腾,当时做出来还是很有成就感的$OvO$ $N<=7$,就是模拟模拟,预处理一下,$over$ $Code$ #include<bit ...

  3. C++ 模板常见特性(函数模板、类模板)

    背景 C++ 是很强大,有各种特性来提高代码的可重用性,有助于减少开发的代码量和工作量. C++ 提高代码的可重用性主要有两方面: 继承 模板 继承的特性我已在前面篇章写过了,本篇主要是说明「模板」的 ...

  4. 基于GMC/umat的复合材料宏细观渐近损伤分析(二)

    采用GMC/umat进行缠绕复合材料力学性能分析,将一些细节分享如下: 1.纤维缠绕复合材料内部交叉及波动分布受缠绕角度.缠绕线形的影响而不同,任意一种纤维缠绕结构其都存在层合区域.螺旋波动区域和环向 ...

  5. 使用PE启动盘清空电脑登入密码

    1.PE启动盘制作过程 要制作一个启动盘可以使用很多工具来制作,比如老毛桃.U深度.大白菜等软件都可以制作PE启动盘.此处就用老毛桃制作PE启动盘为例(http://www.laomaotao.tv/ ...

  6. Vim的环境设定与记录

    vim 会主动将曾经做过的行为记录下来,记录在文件   ~/.viminfo,好方便下次作业. 更改  /etc/vimrc配置操作环境 vim的环境设定参数 :set nu :set  nonu 设 ...

  7. spring boot学习笔记(2)

    Spring boot集成mybatis的三种方式 一.XML文件 在pom文件里面引入mybatis和数据库的依赖 在application.properties中加入数据源配置 其他和ssm配置完 ...

  8. echart两组柱状图对比时,不同类型根据各类型的最大值为基准进行展示

    项目中遇到的问题:因为数据太小,箭头的地方展示不出来,这时的两组对比数据是根据一个最大值为基准进行渲染的.但我们想实现不同类型的对比根据不同的基准值渲染. 理想效果如下图: 实现代码: option ...

  9. VLC播放器的快捷键(shutcut)

    ubuntu上的视频播放器功能简陋,不支持快慢速,于是需要一款播放器来替代它,从网上找了找,大家对VLC的评价出奇的一致, 于是试水了一下,发现功能确实强大,支持大多数多媒体文件以及各类流媒体协议 在 ...

  10. Shell水平测试-想学习Shell的童鞋必选必看文章

    [SHELL水平测试] [OVERVIEW 篇] 有很多种 shell, 你熟悉几种? 各个 shell 的 home page 在那里? 为什么说 zsh 是目前为止功能最为强大的 shell. 为 ...