leetcode 677. Map Sum Pairs
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,Maxsum包括两个操作:
insert:插入字符串和相应的值
sum("xxx"):查询以xxx为前缀的字符串的值的和
思路:
一看就是字典树啊,查询的时候套个dfs就行了
class MapSum {
public:
class node {
public:
node* next[26];
int end;
node() {
for (int i = 0; i < 26; ++i) {
next[i] = nullptr;
}
end = 0;
}
};
node* root;
/** Initialize your data structure here. */
MapSum() {
root = new node();
}
void insert(string key, int val) {
if (key.size() == 0) return ;
node* p = root;
for (int i = 0; i < key.size(); ++i) {
int x = key[i] - 'a';
if (p->next[x] == nullptr) {
p->next[x] = new node();
//p->end += val;
p = p->next[x];
} else {
//p->end += val;
p = p->next[x];
}
}
p->end = val;
}
int sum(string prefix) {
node* p = root;
int sum = 0;
for (int i = 0; i < prefix.size(); ++i) {
int x = prefix[i] - 'a';
if (p->next[x] != nullptr) {
p = p->next[x];
} else {
return false;
}
}
//sum += p->end;
dfs(p, sum);
return sum;
}
void dfs(node *p, int& sum) {
sum += p->end;
for (int i = 0; i < 26; ++i) {
if (p->next[i] != nullptr) {
dfs(p->next[i], sum);
}
}
}
};
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/
leetcode 677. Map Sum Pairs的更多相关文章
- 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 ...
- 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 ...
- [LeetCode] Map Sum Pairs 映射配对之和
Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...
- [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] 1. Two Sum 两数和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
随机推荐
- ⑨要写信(codevs 1697)
题目描述 Description 琪露诺(冰之妖精)有操控冷气的能力.能瞬间冻结小东西,比普通的妖精更危险.一直在释放冷气的她周围总是非常寒冷. 由于以下三点原因…… 琪露诺的符卡 冰符“Icicle ...
- [OS X实用技巧]机器人应用:一键将图片转换为PNG/JPEG/TIFF
转自:http://www.maczhi.com/archives/2842.html 按教程老出错....试验了后使用: - 取得指定Finder对象,其它不变,但运行后不会出错. OS X实用技巧 ...
- Day 8 Linux之Day8
Linux 之 Day 8 一.Linux网络原理及基础设置 1. 使用ifconfig命令来维护网络 1) ifconfig命令的功能:显示所有正在启动的网卡的详细信息或设定系统中网卡的IP地址. ...
- (44)C#网络2
一.用SmtpClient类发送邮件 允许应用程序使用简单邮件传输协议 (SMTP) 发送电子邮件 using System.Net.Mail; SmtpClient smtpClient = new ...
- codevs——1065 01字符串
1065 01字符串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 输出仅有0和1组成的长度为n的字符串, ...
- MongoDB学习day10--数据库导入导出
在 Mongodb 中我们使用 mongodump 命令来备份 MongoDB 数据. 该命令可以导出所有数据到指定目录中.mongodump 命令可以通过参数指定导出的数据量级转存的服务器. 使用m ...
- Spring的Hello World工程
通过Spring的Hello World工程研究以下几个点: 0.如何创建工程及引入依赖. 1.通过Spring的beans.xml实现依赖注入,动态创建实例. 2.了解Spring的工作原理. 具体 ...
- 【Windows7】win7启动 报错 AutoIt错误,不能打开脚本文件
解决方法: 1.开始-->运行-->regedit 2.按照下图 3.
- How to Install a Language Pack
https://www.phpbb.com/kb/article/how-to-install-a-language-pack
- python内存诊断
1.计算内存地址: str1 = 'shn' print id(str1) 2.计算内存大小,返回字节数 str1 = 'td' print sys.getsizeof(str1) 3.