当前位置:网站首页>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); */
边栏推荐
猜你喜欢

hcip实验(12)
![[machine learning] naive Bayesian classification of text -- Classification of people's names and countries](/img/95/1f5b0a17a00da5473180667ccc33e2.png)
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries

DHCP and PPPoE protocols and packet capture analysis

SQL注入 Less38(堆叠注入)

腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实

Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)

HCIP(13)

Learning notes and summary of C language programming specification

静态成员static详解

DHCP和PPPoE协议以及抓包分析
随机推荐
Day3 classification management of Ruiji takeout project
Learning notes and summary of C language programming specification
小程序 组件 定时器的清除
Lvs+keepalived high availability deployment practical application
Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom
Ordinary practice of JS DOM programming
array_ diff_ The method of not comparing array values when Assoc element is an array
Why is 0.1 + 0.2 not equal to 0.3? How to solve this problem?
HCIP(10)
HCIP(12)
Is mov format a still image file format
Ecmasript 5/6 notes
什么是时间复杂度
Bugku,Web:都过滤了
Future trend of defi in bear market
Small program canvas generates posters
LVS+KeepAlived高可用部署实战应用
使用webWorker执行后台任务
Ruiji takeout project - development of business development function Day2
Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)