Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-06-07 11:28:43
※ 引述《Rushia (早瀬ユウカの体操服 )》之铭言:
: https://leetcode.com/problems/replace-words/description
: 648. Replace Words
: 给你一个字串行表dictionary和一个字串sentence,sentence里面有很多单字,这些单字
: 被空白分割,有些单字是从其他单字的字首延伸的例如:helpful = help+ful 若
: sentence里面的单字字首存在于dictionary我们可以把原单字替换成较短的字首,若
: 存在多个字首则取最短,求出替换完的句子长什么样子。
: Example 1:
: Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled
: by the battery"
: Output: "the cat was rat by the bat"
: 思路:
: 1.前面忘了中间忘了凭印象手刻一个字典树,先把所有字根加入字典树。
: 2.接下来把sentence依照空白切成单字,如果这个单字在字典树里面就加入第一个找到的
: 字根,找不到就返回原单字。
: 3.把结果集串起来用空白分割。
: java code
:
作者: SecondRun (雨夜琴声)   2024-06-07 11:29:00
大师
作者: JIWP (JIWP)   2024-06-07 11:30:00
你们都用字典树喔
楼主: sustainer123 (caster)   2024-06-07 11:32:00
找前缀用字典树最直观ㄅ 只是有点难刻
作者: JIWP (JIWP)   2024-06-07 11:32:00
我懒得刻,用hash table
楼主: sustainer123 (caster)   2024-06-07 11:35:00
我顺便复习 不然会忘记怎么刻
作者: JIWP (JIWP)   2024-06-07 11:39:00
也是等下来练习一下好了
作者: wu10200512 (廷廷)   2024-06-07 11:41:00
别卷了
作者: DJYOSHITAKA (Evans)   2024-06-07 11:43:00
别卷了 肥肥不会trie

Links booklink

Contact Us: admin [ a t ] ucptt.com