当前位置:网站首页>Sword finger offer II 066. sum of words (medium prefix tree design string)
Sword finger offer II 066. sum of words (medium prefix tree design string)
2022-07-28 22:25:00 【Calm in the wind and rain】
The finger of the sword Offer II 066. Sum of words
Achieve one MapSum class , Two methods are supported ,insert and sum:
MapSum() initialization MapSum object
void insert(String key, int val) Insert key-val Key value pair , Strings represent keys key , Integers represent values val . If key key Already exist , Then the original key value pair will be replaced by the new key value pair .
int sum(string prefix) Returns all with this prefix prefix Key at the beginning key The sum of the values of .
Example :
Input :
inputs = [“MapSum”, “insert”, “sum”, “insert”, “sum”]
inputs = [[], [“apple”, 3], [“ap”], [“app”, 2], [“ap”]]
Output :
[null, null, 3, null, 5]
explain :
MapSum mapSum = new MapSum();
mapSum.insert(“apple”, 3);
mapSum.sum(“ap”); // return 3 (apple = 3)
mapSum.insert(“app”, 2);
mapSum.sum(“ap”); // return 5 (apple + app = 3 + 2 = 5)
Tips :
1 <= key.length, prefix.length <= 50
key and prefix It's only made up of lowercase letters
1 <= val <= 1000
Call at most 50 Time insert and sum
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/z1R5dt
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
When constructing nodes of prefix tree , Assign values to nodes , In the insert key When , Assign a value at the end node val, stay sum In the method , The shilling node points to the end of the prefix string , And then use it dfs( Here is getSum Method ), Get the values of all the leaf nodes of the subtree .
Answer key (Java)
class MapSum {
static class TrieNode {
public TrieNode[] children;
public int val;
public TrieNode() {
children = new TrieNode[26];
}
}
private TrieNode root;
/** Initialize your data structure here. */
public MapSum() {
root = new TrieNode();
}
public void insert(String key, int val) {
boolean f = false;
TrieNode node = root;
for (char ch : key.toCharArray()) {
if (node.children[ch - 'a'] == null) {
f = true;
node.children[ch - 'a'] = new TrieNode();
}
node = node.children[ch - 'a'];
}
node.val = val;
}
public int sum(String prefix) {
TrieNode node = root;
for (char ch : prefix.toCharArray()) {
if (node.children[ch - 'a'] == null) {
return 0;
}
node = node.children[ch - 'a'];
}
return getSum(node);
}
private int getSum(TrieNode node) {
if (node == null) {
return 0;
}
int ans = node.val;
for (TrieNode child : node.children) {
ans += getSum(child);
}
return ans;
}
}
/** * Your MapSum object will be instantiated and called as such: * MapSum obj = new MapSum(); * obj.insert(key,val); * int param_2 = obj.sum(prefix); */
边栏推荐
猜你喜欢

Win11 how to open software notification

Jmeter 安装第三方插件 Plugins Manager

Intranet penetration learning (III) horizontal movement of domain - planning tasks
![[CS231N]Lecture_ 2:Image Classification pipelin](/img/4f/de56b071560ada746c587a9dbc5f02.jpg)
[CS231N]Lecture_ 2:Image Classification pipelin

Alibaba cloud CDN practice

SQL injection less34 (post wide byte injection + Boolean blind injection)

Hcip experiment (14)

If you want to grow rapidly, you must first experience a major blow!

【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络

Bugku, Web: all filtered
随机推荐
Have you seen the management area decoupling architecture? Can help customers solve big problems
HCIP第七次实验
腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实
array_ diff_ The method of not comparing array values when Assoc element is an array
Future trend of defi in bear market
HCIP(14)
What testing services do third-party software testing institutions provide? Charging standard of software test report
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
阿里云CDN实践
C语言编程规范学习笔记和总结
HCIP(8)
Lvs+keepalived high availability deployment practical application
纪念一下第一次写的线段树了喽(对应洛谷3372)
DHCP和PPPoE协议以及抓包分析
Principle of object. Prototype. ToString. Call()
【NLP】生成词云
HCIP(11)
SQL注入 Less42(POST型堆叠注入)
Win11怎么打开软件通知
Ruiji takeout project - development of business development function Day2