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

  1. 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 ...

  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. [LeetCode] Map Sum Pairs 映射配对之和

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

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

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

  6. [LeetCode] 1. Two Sum 两数和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  9. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

随机推荐

  1. bzoj2850巧克力王国

    巧克力王国 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 861  Solved: 325[Submit][Status][Discuss] Desc ...

  2. bzoj3196 二逼平衡树 树套树(线段树套Treap)

    Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4697  Solved: 1798[Submit][Status][D ...

  3. Spring的声明式事务管理<tx:advice/> 有关的设置

    <tx:advice/> 有关的设置 这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传 ...

  4. ngrinder的安装

    1.官网下载war包(ngrinder-controller),可以使用tomcat启动或者直接nohup java -XX:Permsize=200m -jar ngrinder-3.4.1.war ...

  5. ReSharper7.1.25.234 注册机

    经常用vs做开发的人都知道,ReSharper是vistual studio必备插件之一.他的智能提示,智能感知,.net底层方法查看,测试等都非常方便,给程序员带来了巨大的效率. 但众所周知ReSh ...

  6. dos中定义变量与获取常见的引用变量以及四则运算、备份文件(set用法)

    在dos中使用set定义变量: set  a=8              (注意等号两边没有空格) 引用变量如: echo  %a%        将打印a的值 (%a%是获取变量a的值) dos中 ...

  7. python模块(二)

    一.json模块 作用: 用于[字符串]和 [python基本数据类型] 间进行转换 Python的Json模块序列化与反序列化的过程分别是 encoding和 decoding. encoding ...

  8. elasticsearch入门使用(一)es 6.2.2安装,centos 7

    elasticsearch(一般叫es)是基于Lucene的搜索服务器,提供http协议接口使用json格式数据,也提供相应的客户端,更详细的信息[优点&场景]请百度百科, 以下官网截图,官网 ...

  9. Maven实战:Pom.xml详解

    什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...

  10. shell高级-----初识sed和gawk

    sed编辑器 sed说明 sed是Linux下一款功能强大的非交互流式文本编辑器,可以对文本文件进行增.删.改.查等操作,支持按行.按字段.按正则匹配文本内容,灵活方便,特别适合于大文件的编辑. 替换 ...