当前位置:网站首页>Leetcode - 677 key value mapping (Design)*
Leetcode - 677 key value mapping (Design)*
2022-07-25 15:40:00 【Cute at the age of three @d】


Prefix tree + Hashtable


class MapSum {
class TrieNode{
int val = 0;
TrieNode[] child = new TrieNode[26];
}
private TrieNode root;
private Map<String,Integer> map;
public MapSum() {
root = new TrieNode();
map = new HashMap<>();
}
public void insert(String key, int val) {
int delta = val - map.getOrDefault(key,0);
map.put(key,val);
TrieNode node = root;
for(char c : key.toCharArray()){
if(node.child[c - 'a'] == null)
node.child[c - 'a'] = new TrieNode();
node = node.child[c - 'a'];
node.val += delta;
}
}
public int sum(String prefix) {
TrieNode node = root;
int sumPrefix = 0;
for(char c : prefix.toCharArray()){
if(node.child[c - 'a'] == null)
return 0;
node = node.child[c - 'a'];
}
return node.val;
}
}
/** * Your MapSum object will be instantiated and called as such: * MapSum obj = new MapSum(); * obj.insert(key,val); * int param_2 = obj.sum(prefix); */
边栏推荐
- The difference between mouseover and mouseenter
- 2016CCPC网络选拔赛C-换根dp好题
- 2021江苏省赛A. Array-线段树,维护值域,欧拉降幂
- Binary complement
- The difference between Apple buy in and apple pay
- matlab randint,Matlab的randint函数用法「建议收藏」
- Distributed principle - what is a distributed system
- LeetCode - 707 设计链表 (设计)
- P4552 differential
- 带你创建你的第一个C#程序(建议收藏)
猜你喜欢

CF888G-巧妙字典树+暴力分治(异或最小生成树)

JVM知识脑图分享

伤透脑筋的CPU 上下文切换

LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)

Use cpolar to build a business website (how to buy a domain name)

Take you to create your first C program (recommended Collection)

Games101 review: linear algebra

你准备好脱离“内卷化怪圈”了吗?

谷歌博客:采用多重游戏决策Transformer训练通用智能体

CVPR 2022 | in depth study of batch normalized estimation offset in network
随机推荐
ML - natural language processing - Basics
小波变换--dwt2 与wavedec2
Pat grade a 1153 decode registration card of PAT (25 points)
盒子躲避鼠标
数据系统分区设计 - 分区再平衡(rebalancing)
CF365-E - Mishka and Divisors,数论+dp
LeetCode - 677 键值映射(设计)*
2019浙江省赛C-错排问题,贪心
Leetcode - 622 design cycle queue (Design)
GAMES101复习:线性代数
Cf566a greed + dictionary tree
Cf685b find the center of gravity of each subtree of a rooted tree
MySQL—用户和权限管控
Brain racking CPU context switching
The difference between mouseover and mouseenter
使用cpolar建立一个商业网站(如何购买域名)
二进制补码
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
<栈模拟递归>
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac