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 ...
随机推荐
- API开发之接口安全(一)----生成sign
在对于API的开发中 最让人头疼的 就是接口数据暴露 让一些有心之人 抓包之后恶意请求 那么如何解决这一弊端呢?自然而然的 我们就想到了 加密 那我们又如何加密 如何解密 才能使之有最安全的效率呢? ...
- Beta冲刺版本第二天
该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求地址:https://edu.cnblogs.com ...
- python操作kafka实践
1.先看最简单的场景,生产者生产消息,消费者接收消息,下面是生产者的简单代码. ------------------------------------------------------------ ...
- Selenium(3)
练习1:Ecshop 录制登录后退出业务 打开系统 存储页面的标题 a.点击"登录"按钮 b.输入用户名:testing 存储输入的用户名 c.输入密码:123456 d.点击&q ...
- 牛客小白月赛12 I 华华和月月逛公园 (tarjian 求桥)
链接:https://ac.nowcoder.com/acm/contest/392/I 来源:牛客网 华华和月月逛公园 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K, ...
- vs调试时,不显示局部变量
为了测试一个函数的返回值,就在某个函数里加了一个局部变量,调试却不显示所添加变量的信息. 你一定设置成了release 模式.改为debug就可以了. 比较弱智的问题,mark一下.
- solr admin界面的监控
这里可以看到,solr的版本,lucene的版本,jvm的版本,CPU核数,jvm启动参数,还有物理内存占用,交换空间占用,jvm内存占用. 这里可以看到每个core的情况. 这里可以看到java的所 ...
- pycharm 怎么能像在命令行中输入参数进行调试
pycharm中配置main参数 Run->Edit Configurations->Script Parames 把需要在xxx.py A B C 后面的参数输入到如下位置. 否则会报错 ...
- 数据管理必看!Kendo UI for jQuery过滤器概述
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- PHP swoole websocket协议上机指南
这一上机实验的开发环境用的是jetbrain公司的phpstorm 上级步骤如下: 创建websocket服务端 <?php $server = ); function onopen($serv ...