当前位置:网站首页>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); */
边栏推荐
- Pytorch学习笔记--常用函数总结3
- Pat grade a 1153 decode registration card of PAT (25 points)
- JVM - classloader and parental delegation model
- C#精挑整理知识要点12 异常处理(建议收藏)
- 2019陕西省省赛K-变种Dijstra
- Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem
- 如何实现页面包含
- Use cpolar to build a business website (how to buy a domain name)
- Singleton mode 3-- singleton mode
- 你准备好脱离“内卷化怪圈”了吗?
猜你喜欢

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

ML - natural language processing - Basics

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

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

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

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

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

LeetCode - 677 键值映射(设计)*

Cf888g clever dictionary tree + violent divide and conquer (XOR minimum spanning tree)

Geogle Colab笔记1--运行Geogle云端硬盘上的.py文件
随机推荐
Understanding of this object
Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)
Week303 of leetcode
matlab randint,Matlab的randint函数用法「建议收藏」
LeetCode - 707 设计链表 (设计)
Pat class a topic directory
How to solve cross domain problems
Cf365-e - Mishka and divisors, number theory +dp
Cf566a greed + dictionary tree
Deadlock gossip
使用cpolar建立一个商业网站(如何购买域名)
死锁杂谈
C # fine sorting knowledge points 12 exception handling (recommended Collection)
2019浙江省赛C-错排问题,贪心
Take you to create your first C program (recommended Collection)
SQL cultivation manual from scratch - practical part
C#精挑整理知识要点10 泛型(建议收藏)
微信小程序
2021hncpc-e-difference, thinking
小波变换--dwt2 与wavedec2