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 ...
随机推荐
- 写Java也得了解CPU–CPU缓存
CPU,一般认为写C/C++的才需要了解,写高级语言的(Java/C#/pathon…)并不需要了解那么底层的东西.我一开始也是这么想的,但直到碰到LMAX的Disruptor,以及马丁的博文,才发现 ...
- 第三章·MySQL版本区别及管理
一.MySQL5.6与MySQL5.7安装的区别 1.cmake的时候加入了bostorg 2.初始化时 使用mysqld --initialize 替代mysql_install_db,其它参数没有 ...
- C++ 新特性 笔记
摘录 constexptr C++14尝鲜:constexpr函数(编译期函数) 总结来说,就是在c++11之前,要实现编译期数值计算需要繁琐的模板元编程.在c++11 中,可以是函数,在一句rutu ...
- PAT Basic 1030 完美数列 (25 分)
给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...
- LeetCode03 - 无重复字符的最长子串(Java 实现)
LeetCode03 - 无重复字符的最长子串(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-substri ...
- 查看ocx控件CLSID的方法(转载)
CLSID就是classID类的标识码 1.打开注册表,window + r ,输入regedit,确定 2.点击 编辑 选择查找 3.ok拉 参考:https://blog.csdn.net/u01 ...
- gluOrtho2D与glViewport
https://blog.csdn.net/HouraisanF/article/details/83444183 窗口与显示主要与三个量有关:世界坐标,窗口大小和视口大小.围绕这些量共有4个函数: ...
- cmake 出现undefined reference to xxx 解决办法
cmake没怎么用,主要觉得Clion很好用,但是默认clion使用的是cmake.再说一句clion是linux平台上很好用,个人强推. 当你使用clion的时候,如果使用了thread cstl等 ...
- Vue学习日记(四)——Vue状态管理vuex
前言 先说句前话,如果不是接触大型项目,不需要有多个子页面,不使用vuex也是完全可以的. 说实在话,我在阅读vuex文档的时候,也很难以去理解vuex,甚至觉得没有使用它我也可以.但是直到我在项目碰 ...
- Hadoop-No.14之文件传输的特点
文件传输特点 这是一种all-or-nothing批处理方法,所以如果文件传输过程中出现错误,则不会写入或读取任何数据.这种方法与Flume,Kafka之类的采集方法不同,后者提供一定程度的错误处理功 ...