当前位置:网站首页>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); */
边栏推荐
- LeetCode - 622 设计循环队列 (设计)
- The difference between mouseover and mouseenter
- Get the ask code corresponding to the key pressed by the keyboard
- Cf750f1 thinking DP
- Leetcode - 225 implements stack with queue
- LeetCode - 641 设计循环双端队列(设计)*
- No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
- C#精挑整理知识要点10 泛型(建议收藏)
- 2016CCPC网络选拔赛C-换根dp好题
- Cf566a greed + dictionary tree
猜你喜欢

Gary marcus: learning a language is more difficult than you think

CVPR 2022 | 网络中批处理归一化估计偏移的深入研究

MySQL—常用SQL语句整理总结

Idea - click the file code to automatically synchronize with the directory

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

Pytorch学习笔记--SEResNet50搭建

Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)

GAMES101复习:变换

Pytorch学习笔记--常用函数总结3

JVM knowledge brain map sharing
随机推荐
CF750F1-思维dp
MySQL—常用SQL语句整理总结
LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)
CVPR 2022 | in depth study of batch normalized estimation offset in network
Cf685b find the center of gravity of each subtree of a rooted tree
2019陕西省省赛J-位运算+贪心
2021HNCPC-E-差分,思维
ML - natural language processing - Basics
带你创建你的第一个C#程序(建议收藏)
Deadlock gossip
ICPC2021昆明M-暴力+主席树
IOS interview questions
LeetCode - 622 设计循环队列 (设计)
2021hncpc-e-difference, thinking
LeetCode - 380 O(1) 时间插入、删除和获取随机元素 (设计 哈希表+数组)
伤透脑筋的CPU 上下文切换
Distributed principle - what is a distributed system
How to realize page inclusion
2019浙江省赛C-错排问题,贪心
Week303 of leetcode