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 ...
随机推荐
- 【转】全志A10/A20 Bootloader加载过程分析
原文 : http://blog.csdn.net/allen6268198/article/details/12905425 从这里开始:http://linux-sunxi.org/Bootabl ...
- linux 网卡相关命令
1. ifconfig //查看网络相关信息 2. ifconfig eth0 192.168.1.103 netmask 255.255.255.0 //配置eth0的IP地址 3. route - ...
- Winfrom TextBox 添加水印文字 + 字体颜色
using System.Drawing; using System.Windows.Forms; namespace KK.WaterMark.Control { public partial cl ...
- python常用模块:模块练习
今日作业: 1.简述 什么是模块 模块就将一些函数功能封装在一个文件内,以‘文件名.py’命名,以“import 文件名”方式调用 模块有哪些来源 自定义.内置.DLL编译器.包模块的格式要求有哪些 ...
- python函数:函数使用原则、定义与调用形式
一.函数初始 二.函数的使用原则 三.函数的定义与调用形式 四.函数的返回值 五.函数参数的使用 一.函数初始 # 须知一: # 硬盘空间无法修改,硬盘中的数据更新都是用新的内容覆盖旧的内容 # 内存 ...
- Linux终端命令行的快捷键
涉及在Linux命令行下进行快速移动光标.命令编辑.编辑后执行历史命令.Bang(!)命令.控制命令等.让basher更有效率. • 常用 1.ctrl+左右键:在单词之间跳转 2.ctrl+a:跳到 ...
- Hive动态分区和分桶(八)
Hive动态分区和分桶 1.Hive动态分区 1.hive的动态分区介绍 hive的静态分区需要用户在插入数据的时候必须手动指定hive的分区字段值,但是这样的话会导致用户的操作复杂度提高,而且在 ...
- MYSQL获得查询记录的行号
对于获得查询记录的行号这一功能,Oracle 中可以使用标准方法(8i版本以上),也可以使用非标准的ROWNUM,MS SQL Server 则在 2005 版本中提供了ROW_NUMBER()函数. ...
- impdp导入报错39002
原文:https://www.cnblogs.com/huacw/p/3888807.html 1 create directory data_pump_dir as '\exphd\datapump ...
- 01- ES6、jquery源码、node、webpack
1.课程介绍 小马哥blog:https://www.cnblogs.com/majj/ 前端学习路径:https://www.processon.com/view/link/5d3a5947e4b0 ...