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 ...
随机推荐
- linux wireless 基础知识 MAC80211 CFG80211
转:http://blog.csdn.net/liuxd3000/article/details/23761663 1. 基本概念 • cfg80211: 用于对无线设备进行配置管理.与Full ...
- three.js之性能监视器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CentOS7.x卸载与安装MySQL5.7的操作过程以及编码格式的修改
一.MySQL5.7的卸载 1.1yum方式查看yum是否安装过mysql cd yum list installed mysql* 如或显示了列表,说明系统中有MySQL 如上显示,我已经安装了my ...
- Linux系统组成和获取命令帮助3
命令的语法通用格式: # COMMAND OPTIONS ARGUMENTS COMMAND: 发起一个命令:请求内核将某个二进制程序运行为一个进程 ...
- LeetCode3.无重复字符的最大子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...
- [转]DSL-让你的 Ruby 代码更优秀
https://ruby-china.org/topics/38428 以下摘录 DSL和Gpl DSL : domain-specific language.比如HTML是用于组织网页的‘语言’, ...
- 部署nginx脚本
cd nginx-1.12.2useradd -s /sbin/nologin nginx./configuremakemake installyum -y install mariadb maria ...
- 《Python基础教程》第一章:基础知识
如果希望只执行普通的除法,可以在程序前加上以下语句:from __future__ import division.还有另外一个方法,如果通过命令行运行Python, 可以使用命令开关-Qnew.此时 ...
- 简易MySQL存储过程
自从那天灵感突现,搜了下MySQL存储过程的实现,我就再也不会为造测试数据这种事情烦恼了,存储过程用起来简直太方便了. DROP PROCEDURE IF EXISTS insert2pay; DEL ...
- python的方法VSjava方法
java 类方法和实例方法 类方法 用static修饰的方法. 由于类方法是属于整个类的,所以类方法的方法体中不能有与类的对象有关的内容. 即类方法体有如下限制: 1.类方法中不能引用对象变量: 2. ...