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:

  1. Input: insert("apple", 3), Output: Null
  2. Input: sum("ap"), Output: 3
  3. Input: insert("app", 2), Output: Null
  4. Input: sum("ap"), Output: 5

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

Trie简单题。

  1. #include <assert.h>
  2. #include <map>
  3. #include <iostream>
  4. #include <vector>
  5. #include <unordered_map>
  6. #include <algorithm>
  7. #define ALL(x) (x).begin(), (x).end()
  8. using namespace std;
  9.  
  10. struct TrieNode{
  11. TrieNode* children[];
  12. string word;
  13. int sum;
  14. TrieNode(){
  15. for(int i=; i<; i++) children[i] = nullptr;
  16. word = "";
  17. sum = ;
  18. }
  19. };
  20.  
  21. class Trie{
  22. TrieNode* root;
  23. public:
  24. Trie(){
  25. root = new TrieNode();
  26. }
  27. explicit Trie(vector<string> words, vector<int> value){
  28. root = new TrieNode();
  29. buildtrie(words, value);
  30. }
  31. void buildtrie(vector<string>& words, vector<int>& value){
  32. for(int i=; i<words.size(); i++){
  33. TrieNode* tmp = root;
  34. for(int j=; j<words[i].size(); j++){
  35. int idx = words[i][j] - 'a';
  36. if(!tmp->children[idx]) tmp->children[idx] = new TrieNode();
  37. tmp = tmp->children[idx];
  38. tmp->sum += value[i];
  39. }
  40. tmp->word = words[i];
  41. }
  42. }
  43. int getprefixsum(string prefix){
  44. TrieNode* tmp = root;
  45. for(int i=; i<prefix.size(); i++){
  46. int idx = prefix[i] - 'a';
  47. if(!tmp->children[idx]) return ;
  48. tmp = tmp->children[idx];
  49. }
  50. return tmp->sum;
  51. }
  52. };
  53.  
  54. class MapSum {
  55. private:
  56. unordered_map<string, int> mp;
  57. Trie trie;
  58. public:
  59. /** Initialize your data structure here. */
  60. MapSum() {
  61. trie = Trie();
  62. }
  63.  
  64. void insert(string key, int val) {
  65. if(!mp.count(key)){
  66. mp[key] = val;
  67. vector<string> words = {key};
  68. vector<int> valvec = {val};
  69. trie.buildtrie(words,valvec);
  70. }else{
  71. vector<string> words = {key};
  72. vector<int> valvec = {val - mp[key]};
  73. trie.buildtrie(words, valvec);
  74. }
  75. }
  76.  
  77. int sum(string prefix) {
  78. return trie.getprefixsum(prefix);
  79. }
  80. };

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. 写Java也得了解CPU–CPU缓存

    CPU,一般认为写C/C++的才需要了解,写高级语言的(Java/C#/pathon…)并不需要了解那么底层的东西.我一开始也是这么想的,但直到碰到LMAX的Disruptor,以及马丁的博文,才发现 ...

  2. 第三章·MySQL版本区别及管理

    一.MySQL5.6与MySQL5.7安装的区别 1.cmake的时候加入了bostorg 2.初始化时 使用mysqld --initialize 替代mysql_install_db,其它参数没有 ...

  3. C++ 新特性 笔记

    摘录 constexptr C++14尝鲜:constexpr函数(编译期函数) 总结来说,就是在c++11之前,要实现编译期数值计算需要繁琐的模板元编程.在c++11 中,可以是函数,在一句rutu ...

  4. PAT Basic 1030 完美数列 (25 分)

    给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...

  5. LeetCode03 - 无重复字符的最长子串(Java 实现)

    LeetCode03 - 无重复字符的最长子串(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-substri ...

  6. 查看ocx控件CLSID的方法(转载)

    CLSID就是classID类的标识码 1.打开注册表,window + r ,输入regedit,确定 2.点击 编辑 选择查找 3.ok拉 参考:https://blog.csdn.net/u01 ...

  7. gluOrtho2D与glViewport

    https://blog.csdn.net/HouraisanF/article/details/83444183 窗口与显示主要与三个量有关:世界坐标,窗口大小和视口大小.围绕这些量共有4个函数: ...

  8. cmake 出现undefined reference to xxx 解决办法

    cmake没怎么用,主要觉得Clion很好用,但是默认clion使用的是cmake.再说一句clion是linux平台上很好用,个人强推. 当你使用clion的时候,如果使用了thread cstl等 ...

  9. Vue学习日记(四)——Vue状态管理vuex

    前言 先说句前话,如果不是接触大型项目,不需要有多个子页面,不使用vuex也是完全可以的. 说实在话,我在阅读vuex文档的时候,也很难以去理解vuex,甚至觉得没有使用它我也可以.但是直到我在项目碰 ...

  10. Hadoop-No.14之文件传输的特点

    文件传输特点 这是一种all-or-nothing批处理方法,所以如果文件传输过程中出现错误,则不会写入或读取任何数据.这种方法与Flume,Kafka之类的采集方法不同,后者提供一定程度的错误处理功 ...