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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - 司徒正美

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 677. Map Sum Pairs leetcode 648. Replace Words
leetcode 676. Implement Magic Dictionary
司徒正美 · 2019-12-27 · via 博客园 - 司徒正美

使用Tire 处理

function Node(value) {
      this.word = null
      this.children = {}
    }
    class MagicDictionary {
      constructor() {
        this.root = new Node(null)
      }
      addWord(word) {
        var node = this.root;
        for (var i = 0; i < word.length; i++) {
          var next = word[i]
          if (!node.children[next]) {
            node.children[next] = new Node()
          }
          node = node.children[next]
        }
        node.isEnd = true
      }
      buildDict(words) {
        for (let word of words) {
          this.addWord(word)
        }
      }
      search(word) {
        var v = !!searchInner(this.root, word, 0, 0);
        console.log(v);
        return v
      }
    }

    function searchInner(node, word, index, flag) {
      if (index < word.length) {
        var cur = word[index]
        if (node.children[cur]) {
          if (searchInner(node.children[cur], word, index + 1, flag)) {
            return true
          }
        }
        if (!flag) {
          for (let c in node.children) {
            if (c !== cur && searchInner(node.children[c], word, index + 1, true)) {
              return true
            }
          }
        }
        return false
      }
      return (flag && node.isEnd);
    }



    var tire = new MagicDictionary()
    tire.buildDict(["hello", "leetcode"])
    tire.search('hello')
    tire.search('hhllo')
    tire.search('hell')
    tire.search('leetcoded')


    var tire1 = new MagicDictionary()
    tire1.buildDict(["hello", "hallo", "leetcode"])
    tire1.search('hello')
    tire1.search('hhllo')
    tire1.search('hell')
    tire1.search('leetcodd')