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. 刷题总结——射箭(bzoj2732)

    题目: Description 沫沫最近在玩一个二维的射箭游戏,如下图 1 所示,这个游戏中的 x 轴在地面,第一象限中有一些竖直线段作为靶子,任意两个靶子都没有公共部分,也不会接触坐标轴.沫沫控制一 ...

  2. NIO Channel的学习笔记总结

    摘自:http://blog.csdn.net/tsyj810883979/article/details/6876603 1.1  非阻塞模式 Java  NIO非堵塞应用通常适用用在I/O读写等方 ...

  3. 【THUSC2016】成绩单(bzoj4897)

    $f(i,j,x,y)$ 表示区间 $[i,j]$中,第 $j$ 个数在最后一次操作中才消去,最后一次操作的最大值为 $x$,最小值为 $y$ 时的最小代价: $g(i,j)$ 表示区间 $[i,j] ...

  4. SQL Server中的@@ROWCOUNT

    SQL Server中@@ROWCOUNT返回受上一语句影响的行数,返回值类型为 int 整型. 如果行数大于 20 亿,则需要使用 ROWCOUNT_BIG. @@ROWCOUNT和@@ERROR变 ...

  5. POJ 3099 Go Go Gorelians

    http://poj.org/problem?id=3099 树的重心:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心 求树的重心 如何在点中构造符合条件的树 得到树后 从 ...

  6. AtCoder Regular Contest 074F - Lotus Leaves

    $n \leq 300,m \leq 300$,$n*m$的格子里有起点有终点有空地有障碍,人会从起点选一个同行或同列空地跳过去,然后一直这样跳到终点.求至少删掉多少格子使得人跳不到终点. 首先S和T ...

  7. SQL 随机取出一条数据

    今天遇到一需求,需要随机取出一条数据.网上找了下,sqlserver自带的有newID()这个函数,可以随机出来一个guid,用来取随机数还是蛮不错的. 直接上SQL: select top 1 *, ...

  8. Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp

    D - MUH and Cube Walls Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  9. spring启动时加载字典表数据放入map

    import java.util.HashMap; import java.util.List; import org.springframework.beans.factory.annotation ...

  10. 【js html】对于<img>图片的引用填充,src可以给什么值?

    平时多见的<img>的使用,常见于如下: <img class="img-responsive img-rounded" src="static/img ...