当前位置:网站首页>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); */
边栏推荐
- matlab randint,Matlab的randint函数用法「建议收藏」
- MySQL optimization summary II
- mouseover和mouseenter的区别
- 2021上海市赛-D-卡特兰数变种,dp
- C # fine sorting knowledge points 9 Set 2 (recommended Collection)
- LeetCode - 677 键值映射(设计)*
- JVM knowledge brain map sharing
- 2021HNCPC-E-差分,思维
- C # fine sorting knowledge points 10 generic (recommended Collection)
- LeetCode - 622 设计循环队列 (设计)
猜你喜欢

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

CVPR 2022 | in depth study of batch normalized estimation offset in network

Leetcode - 641 design cycle double ended queue (Design)*

Box avoiding mouse

Window system black window redis error 20creating server TCP listening socket *: 6379: listen: unknown error19-07-28

p4552-差分

wait()和sleep()的区别理解

Games101 review: Transformation

Leetcode - 622 design cycle queue (Design)

LeetCode - 359 日志速率限制器 (设计)
随机推荐
Wechat applet
我想问下变量配置功能是只能在SQL模式下使用吗
Pat class a topic directory
LeetCode - 622 设计循环队列 (设计)
伤透脑筋的CPU 上下文切换
C#精挑整理知识要点11 委托和事件(建议收藏)
GAMES101复习:线性代数
wait()和sleep()的区别理解
PageHelper does not take effect, and SQL does not automatically add limit
LeetCode - 232 用栈实现队列 (设计 双栈实现队列)
var、let、const之间的区别
ZOJ - 4114 Flipping Game-dp,合理状态表示
LeetCode - 380 O(1) 时间插入、删除和获取随机元素 (设计 哈希表+数组)
PAT甲级题目目录
Games101 review: 3D transformation
2021上海市赛-H-二分答案
Google Blog: training general agents with multi game decision transformer
2021 Shanghai match-h-two point answer
自定义注解校验API参数电话号
Understanding of this object