当前位置:网站首页>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); */
边栏推荐
- 2016CCPC网络选拔赛C-换根dp好题
- Application of C language array in Sanzi chess -- prototype of Queen n problem
- 2021 Jiangsu race a Array line segment tree, maintain value range, Euler power reduction
- Hdu3873 shortest path with dependency (topological sorting)
- Flex layout
- Singleton mode 3-- singleton mode
- Qtime定义(手工废物利用简单好看)
- CF888G-巧妙字典树+暴力分治(异或最小生成树)
- C#精挑整理知识要点11 委托和事件(建议收藏)
- PAT甲级题目目录
猜你喜欢

SVD奇异值分解推导及应用与信号恢复

ML - natural language processing - Introduction to natural language processing

ML - 语音 - 语音处理介绍

ML - natural language processing - Basics

p4552-差分

Geogle Colab笔记1--运行Geogle云端硬盘上的.py文件
SQL cultivation manual from scratch - practical part

GAMES101复习:变换

ML - Speech - advanced speech model

Node learning
随机推荐
mouseover和mouseenter的区别
Local cache --ehcache
死锁杂谈
PAT甲级1152 Google Recruitment (20 分)
Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果
Find out what happened in the process of new
Singleton mode 3-- singleton mode
UIDocumentInteractionController UIDocumentPickerViewController
2016CCPC网络选拔赛C-换根dp好题
BPSK调制系统MATLAB仿真实现(1)
MySQL optimization summary II
Brain racking CPU context switching
Get the ask code corresponding to the key pressed by the keyboard
获取键盘按下的键位对应ask码
User defined annotation verification API parameter phone number
PAT甲级题目目录
Cf750f1 thinking DP
2019 Shaanxi provincial competition j-bit operation + greed
ML - natural language processing - Introduction to natural language processing
Pytorch学习笔记--常用函数总结2