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

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Map Sum Pairs.

Trie简单题。

#include <assert.h>
#include <map>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#define ALL(x) (x).begin(), (x).end()
using namespace std; struct TrieNode{
TrieNode* children[];
string word;
int sum;
TrieNode(){
for(int i=; i<; i++) children[i] = nullptr;
word = "";
sum = ;
}
}; class Trie{
TrieNode* root;
public:
Trie(){
root = new TrieNode();
}
explicit Trie(vector<string> words, vector<int> value){
root = new TrieNode();
buildtrie(words, value);
}
void buildtrie(vector<string>& words, vector<int>& value){
for(int i=; i<words.size(); i++){
TrieNode* tmp = root;
for(int j=; j<words[i].size(); j++){
int idx = words[i][j] - 'a';
if(!tmp->children[idx]) tmp->children[idx] = new TrieNode();
tmp = tmp->children[idx];
tmp->sum += value[i];
}
tmp->word = words[i];
}
}
int getprefixsum(string prefix){
TrieNode* tmp = root;
for(int i=; i<prefix.size(); i++){
int idx = prefix[i] - 'a';
if(!tmp->children[idx]) return ;
tmp = tmp->children[idx];
}
return tmp->sum;
}
}; class MapSum {
private:
unordered_map<string, int> mp;
Trie trie;
public:
/** Initialize your data structure here. */
MapSum() {
trie = Trie();
} void insert(string key, int val) {
if(!mp.count(key)){
mp[key] = val;
vector<string> words = {key};
vector<int> valvec = {val};
trie.buildtrie(words,valvec);
}else{
vector<string> words = {key};
vector<int> valvec = {val - mp[key]};
trie.buildtrie(words, valvec);
}
} int sum(string prefix) {
return trie.getprefixsum(prefix);
}
};

LC 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. 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)

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

  3. leetcode 677. Map Sum Pairs

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

  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. python练习笔记——map | sum | pow 的应用

    1 函数简要 map 函数  | sum 函数  |  pow函数  | lambda函数 2 简要计算 2.1 1^2 + 2^2 + 3^2 .....9^2 方法1 print([pow(x,2 ...

  7. [LC] 437. Path Sum III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. LC 918. Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  9. LC 1. Two Sum

    题目介绍 Given an array of integers, return indices of the two numbers such that they add up to a specif ...

随机推荐

  1. GNU编译器:Codesourcery

    Codesourcery G++是个商业软件, 不过它有个lite版本,是完全免费的,只不过没有IDE,只有commmand line. Codesourcery G++支持coldfire, pow ...

  2. Oracle【子查询】

    Oracle子查询:当一个查询依赖于另外一个查询的结果的时候,就需要使用子查询.单行子查询 :筛选条件不明确,需要执行一次查询且查询结果只有一个字段且字段值只有一个.注意:where子句中允许出现查询 ...

  3. docker 运行centos显示连不上网络

    centos 7 docker 启动了一个web服务 但是启动时 报 WARNING: IPv4 forwarding is disabled. Networking will not work. 网 ...

  4. 解决canvas图片getImageData,toDataURL跨域问题

    图片服务器需要配置Access-Control-Allow-Origin 当需要需要对canvas图片进行getImageData()或toDataURL()操作的时候,跨域问题就出来了.图片服务器需 ...

  5. zencart简易页面ezpage后台编辑位置

    zencart简易页面ezpage后台编辑位置: 后台-Tools(工具)-EZ-Pages(简易页面管理) 若编辑之后无法保存,说明includes\languages\语言包\html_inclu ...

  6. Summer training #2

    A:不管有没有负数 一顿操作之后肯定只有正数 又因为A=A-B 所以最大值是一直在减小的 所以一定有结果 B:..一开始以为求min操作数 WA了2发 直接求所有数的GCD如果所有数的GCD都不是1的 ...

  7. 关于注解-Hebernate与JPA(java persistence api)

    The JPA spec. defines the JPA annotation in the javax.persistence package. Hibernate not only implem ...

  8. SpringBoot读取Resource下文件的几种方式

    https://www.jianshu.com/p/7d7e5e4e8ae3 最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传:这里模板位置resour ...

  9. 检验注解@Validated 和@valid说明

  10. 【洛谷P2387】魔法森林

    题目大意:给定一个 N 个点,M 条边的无向图,边有两个边权 a, b,求从 1 号节点到 N 号节点路径的两个权值和的最大值最小是多少. 题解: 对于有两个属性的结构的最优化问题,可以考虑先按照其中 ...