1 Star 0 Fork 0

wd6/LeetCode-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
map-sum-pairs.cpp 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 8年前 . Update map-sum-pairs.cpp
// Time: O(n), n is the length of key
// Space: O(t), t is the number of nodes in trie
class MapSum {
public:
/** Initialize your data structure here. */
MapSum() {
}
void insert(string key, int val) {
trie_.Insert(key, val);
}
int sum(string prefix) {
return trie_.GetCount(prefix);
}
private:
struct TrieNode {
bool isString = false;
int count = 0;
int val = 0;
unordered_map<char, TrieNode *> leaves;
void Insert(const string& s, const int val) {
const auto delta = val - GetVal(s);
auto* p = this;
for (const auto& c : s) {
if (p->leaves.find(c) == p->leaves.cend()) {
p->leaves[c] = new TrieNode;
}
p = p->leaves[c];
p->count += delta;
}
p->val = val;
p->isString = true;
}
int GetVal(const string& s) const {
auto* p = this;
for (const auto& c : s) {
if (p->leaves.find(c) == p->leaves.cend()) {
return 0;
}
p = p->leaves.at(c);
}
return p->isString ? p->val : 0;
}
int GetCount(const string& s) const {
auto* p = this;
for (const auto& c : s) {
if (p->leaves.find(c) == p->leaves.cend()) {
return 0;
}
p = p->leaves.at(c);
}
return p->count;
}
~TrieNode() {
for (auto& kv : leaves) {
if (kv.second) {
delete kv.second;
}
}
}
};
TrieNode trie_;
};
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wd6/LeetCode-1.git
git@gitee.com:wd6/LeetCode-1.git
wd6
LeetCode-1
LeetCode-1
master

搜索帮助