LC 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
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的更多相关文章
- 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 ...
- 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...
- leetcode 677. Map Sum Pairs
Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- LC 1. Two Sum
题目介绍 Given an array of integers, return indices of the two numbers such that they add up to a specif ...
随机推荐
- JavaMaven【三、常用指令】
mvn compile --编译,编译后生成target文件,里面包含classes mvn test --执行test,测试后在target下生成reports文件夹,测试报告 mvn packag ...
- 如何处理Win10电脑黑屏后出现代码0xc0000225的错误?
有些Win10系统的用户反映电脑在开机的时候突然变成黑屏,还出现提示0xc0000225的错误代码,不知道该怎么去解决.一般来说,遇到这种情况一般是系统的注册表出现了问题.下面就为大家分享一下相应的解 ...
- Delphi ADO组件
樊伟胜
- Asp.Net MVC5 使用Unity 实现依赖注入
到这里安装完毕会提示一个redme.txt,说 把 UnityConfig.RegisterComponents(); 放到下图的位置,我们照做即可. 然后我们看一下这个 UnityConfig ...
- Webpack编译提示内存溢出解决方案
在项目开发中,随着业务需求的复杂项目随之增大,再加上同一个文件被引用次数过于频繁在开发编译或者上线打包时经常会出现如下错误: 这个报错的意思就是Node内存不足所导致的,我们都知道 Node 是基于V ...
- Hdu 2147 巴什博弈 PN图
P:先手必输 N:先手必胜 PN图规则:如果一个点能转换到的所有点都是N,那么此点状态为P.若能转化到一个P,即使对手面临必输状态,此点为N. 然后找规律就行 #include<bits/s ...
- Linux用户账号文件——passwd
/etc/passwd文件是UNIX安全的关键文件之一.该文件用于用户登录时校验用户的登录名.加密的口令数据项.用户ID(UID).默认的用户组ID(GID).用户信息.用户主目录以及登录后使用的sh ...
- idea详细设置:编码、代码提示大小写、窗口数量限制、自动导包、serialID、重复代码警告、热部署等设置
提示: idea ultimate 2018.2 idea-file-setttings设置的是当前项目的配置(只针对当前项目生效)idea-file-others settings相当于以后导入创建 ...
- 初识LVS和LVS_NAT
如果一台服务器承受过多的压力,那么服务可能会崩溃,所以,我们应该让一台服务器承受的压力在合理范围内,但是如果服务端必须要承受较大的压力,那么一台服务器可能无法满足我们的要求,所以我们可以使用多台服务器 ...
- 使用OmniDiskSweeper清理MAC
Mac 经常提示我磁盘空间已满,管理磁盘空间. 然后我就管理了一下,发现系统竟占90个G,有点懵逼.然后网上查了资料 使用了一个名叫OmniDiskSweeper的超级强大的工具,而且还是免费的,它能 ...