当前位置:网站首页>LeetCode - 677 键值映射(设计)*
LeetCode - 677 键值映射(设计)*
2022-07-25 15:32:00 【三岁就很萌@D】


前缀树+哈希表


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 - 379 电话目录管理系统(设计)

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

4PAM在高斯信道与瑞利信道下的基带仿真系统实验

matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值

BPSK调制系统MATLAB仿真实现(1)

ML - 图像 - 深度学习和卷积神经网络

Phased summary of the research and development of the "library management system -" borrowing and returning "module

谷歌云盘如何关联Google Colab

LeetCode - 225 用队列实现栈

GAMES101复习:变换
随机推荐
ZOJ - 4114 flipping game DP, reasonable state representation
Pytorch学习笔记--常用函数总结3
ML - Speech - advanced speech model
Singleton mode 3-- singleton mode
wait()和sleep()的区别理解
ML - 图像 - 深度学习和卷积神经网络
LeetCode - 622 设计循环队列 (设计)
Graph theory and concept
CF365-E - Mishka and Divisors,数论+dp
2021hncpc-e-difference, thinking
Endnote 添加中文GBT7714样式 word中如何引用文献
使用cpolar建立一个商业网站(如何购买域名)
LeetCode - 379 电话目录管理系统(设计)
C#精挑整理知识要点10 泛型(建议收藏)
Cf888g clever dictionary tree + violent divide and conquer (XOR minimum spanning tree)
PAT甲级1152 Google Recruitment (20 分)
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
ML - 自然语言处理 - 自然语言处理简介
JS URLEncode function
Binary complement