惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 司徒正美

leetcode 91. Decode Ways leetcode 1214 Two Sum BSTs leetcode 213 House Robber II leetcode 198 House Robber I leetcode 986. Interval List Intersections leetcode 869. Reordered Power of 2 leetcode 925. Long Pressed Name leetcode 457. Circular Array Loop leetcode 1093. Statistics from a Large Sample leetcode 881. Boats to Save People leetcode 977. Squares of a Sorted Array leetcode 844. Backspace String Compare leetcode 1032. Stream of Characters leetcode 1023. Camelcase Matching leetcode 745 Prefix and Suffix Search leetcode 720. Longest Word in Dictionary leetcode 692. Top K Frequent Words leetcode 676. Implement Magic Dictionary leetcode 648. Replace Words
leetcode 677. Map Sum Pairs
司徒正美 · 2019-12-28 · via 博客园 - 司徒正美

使用前缀树

 function Node() {
      this.value = 0
      this.children = {}
    }
    class MapSum {
      constructor() {
        this.root = new Node()
      }
      insert(word, val) {
        var node = this.root;
        for (let next of word) {
          if (!node.children[next]) {
            node.children[next] = new Node()
          }
          node = node.children[next]
          node.value += val
        }
        node.value += val
        node.isEnd = true
      }
      sum(word) {
        var node = this.root;
        for (let next of word) {
          node = node.children[next]
        }
        return node && node.value
      }
    }

    var tire = new MapSum()
    tire.insert("apple", 3)
    console.log(tire.sum("ap"))
    tire.insert("app", 2);
    console.log(tire.sum("ap"))

或者 使用map优化一下

    var MapSum = function () { // Space: O(n)
      this.map = new Map();
      this.root = new TrieNode();
    };

    /** 
     * @param {string} key 
     * @param {number} val
     * @return {void}
     */
    MapSum.prototype.insert = function (key, val) { // Time: O(k), k = leng(key)
      const delta = val - (this.map.get(key) || 0);
      this.map.set(key, val);
      let node = this.root;
      node.score += delta;
      for (const char of key) {
        if (!node.children.has(char)) {
          node.children.set(char, new TrieNode());
        }
        node = node.children.get(char);
        node.score += delta;
      }
    };

    /** 
     * @param {string} prefix
     * @return {number}
     */
    MapSum.prototype.sum = function (prefix) { // Time: O(k), k = len(prefix)
      let node = this.root;
      for (const char of prefix) {
        node = node.children.get(char);
        if (!node) return 0;
      }
      return node.score;
    };

    function TrieNode() {
      this.children = new Map();
      this.score = 0;
    }