Map Sum Pairs问题及解法
2017-09-18 15:42
344 查看
问题描述:
Implement a MapSum class with
For the method
the key and the integer represents the value. If the key already existed, then the original key-value pair will be overridden to the new one.
For the method
return the sum of all the pairs' value whose key starts with the prefix.
示例:
问题分析:
对于含有公共前缀的字符串问题,我们可以考虑利用Trie数据结构,将字符串存储起来,每次求和时,若给定的公共前缀不存在,直接返回0;若存在,可以递归遍历出所有含有公共前缀的字串对应值的和。
过程详见代码:
class Trie{
public:
Trie * children[26];
bool isLeaf;
int val;
public:
Trie()
{
for (int i = 0; i < 26; i++) children[i] = nullptr;
isLeaf = false;
val = 0;
}
};
class MapSum {
public:
Trie * root;
/** Initialize your data structure here. */
MapSum() {
root = new Trie();
}
void insert(string key, int val) {
if (key == "") return;
Trie * tRoot = root;
for (int i = 0; i < key.length(); i++)
{
if (tRoot->children[key[i] - 'a'] == nullptr)
{
tRoot->children[key[i] - 'a'] = new Trie();
}
tRoot = tRoot->children[key[i] - 'a'];
}
tRoot->isLeaf = true;
tRoot->val = val;
}
int sum(string prefix) {
Trie * tRoot = root;
int res = 0;
for (int i = 0; i < prefix.length(); i++)
{
if (tRoot->children[prefix[i] - 'a'] != nullptr)
tRoot = tRoot->children[prefix[i] - 'a'];
else return 0;
}
blsum(tRoot, res);
return res;
}
void blsum(Trie * tRoot, int& res)
{
if (tRoot == nullptr) return;
if (tRoot->isLeaf) res += tRoot->val;
for (int i = 0; i < 26; i++)
{
blsum(tRoot->children[i], res);
}
}
};
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/
Implement a MapSum class with
insert, and
summethods.
For the method
insert, you'll be given a pair of (string, integer). The string represents
the key and the integer represents the value. If the key already existed, then the original key-value pair will be overridden to the new one.
For the method
sum, you'll be given a string representing the prefix, and you need to
return the sum of all the pairs' value whose key starts with the prefix.
示例:
Input: insert("apple", 3), Output: Null Input: sum("ap"), Output: 3 Input: insert("app", 2), Output: Null Input: sum("ap"), Output: 5
问题分析:
对于含有公共前缀的字符串问题,我们可以考虑利用Trie数据结构,将字符串存储起来,每次求和时,若给定的公共前缀不存在,直接返回0;若存在,可以递归遍历出所有含有公共前缀的字串对应值的和。
过程详见代码:
class Trie{
public:
Trie * children[26];
bool isLeaf;
int val;
public:
Trie()
{
for (int i = 0; i < 26; i++) children[i] = nullptr;
isLeaf = false;
val = 0;
}
};
class MapSum {
public:
Trie * root;
/** Initialize your data structure here. */
MapSum() {
root = new Trie();
}
void insert(string key, int val) {
if (key == "") return;
Trie * tRoot = root;
for (int i = 0; i < key.length(); i++)
{
if (tRoot->children[key[i] - 'a'] == nullptr)
{
tRoot->children[key[i] - 'a'] = new Trie();
}
tRoot = tRoot->children[key[i] - 'a'];
}
tRoot->isLeaf = true;
tRoot->val = val;
}
int sum(string prefix) {
Trie * tRoot = root;
int res = 0;
for (int i = 0; i < prefix.length(); i++)
{
if (tRoot->children[prefix[i] - 'a'] != nullptr)
tRoot = tRoot->children[prefix[i] - 'a'];
else return 0;
}
blsum(tRoot, res);
return res;
}
void blsum(Trie * tRoot, int& res)
{
if (tRoot == nullptr) return;
if (tRoot->isLeaf) res += tRoot->val;
for (int i = 0; i < 26; i++)
{
blsum(tRoot->children[i], res);
}
}
};
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/
相关文章推荐
- Path Sum II问题及解法
- leetcode刷题,我的解法1 twosum问题
- Combination Sum IV问题及解法
- Most Frequent Subtree Sum问题及解法
- Combination Sum II问题及解法
- Minimum ASCII Delete Sum for Two Strings问题及解法
- 677. Map Sum Pairs
- Two Sum II - Input array is sorted问题及解法
- Target Sum问题及解法
- 第14周Map Sum Pairs
- LWC 50:677. Map Sum Pairs
- 【Leetcode】416-Partition Equal Subset Sum -- javascript解法 01背包问题
- Swap Nodes in Pairs问题及解法
- Range Sum Query - Immutable问题及解法
- JavaScript的two-sum问题解法
- Map Sum Pairs
- Continuous Subarray Sum问题及解法
- Minimum Index Sum of Two Lists问题及解法
- ※ Leetcode - Segment Tree - 307. Range Sum Query - Mutable (线段树+树状数组两种解法以及模板的常见问题解析)
- K-diff Pairs in an Array问题及解法