跳转到指定楼层
上一主题 下一主题
收起左侧

Pinterest新鲜上门挂经

🔗
 楼主| 泡芙小姐的金鱼 2018-10-24 11:35:42 | 只看该作者
全局:
落魄酸丁 发表于 2018-10-24 02:00
楼主什么时候面的啊?

昨天紫薯紫薯紫薯
回复

使用道具 举报

🔗
 楼主| 泡芙小姐的金鱼 2018-10-24 11:36:00 | 只看该作者
全局:
Tyrant89 发表于 2018-10-24 07:16
就是建立个Trie,但不同的是我TRie里面的每个元素是单词不是字母

是的我也是用的word Trie~
回复

使用道具 举报

🔗
 楼主| 泡芙小姐的金鱼 2018-10-24 11:36:35 | 只看该作者
全局:
FML 发表于 2018-10-24 07:35
这题是不是就是建树的意思呀

不会出现的,就是直接上级,一个多叉树
回复

使用道具 举报

🔗
 楼主| 泡芙小姐的金鱼 2018-10-24 11:36:47 | 只看该作者
全局:
pandami 发表于 2018-10-24 07:29
第三轮是说有很多string求最长共同prefix?

是的是的~
回复

使用道具 举报

🔗
keepgoing 2018-10-24 12:06:16 | 只看该作者
全局:
泡芙小姐的金鱼 发表于 2018-10-24 11:36
是的我也是用的word Trie~

想请教一下要是对blacklist建word trie的话,应该还需要对Pintext里的每个单词作为开头search trie吧?
回复

使用道具 举报

🔗
金妮韦崽 2018-10-24 13:19:25 | 只看该作者
全局:
楼主是网申还是怎样的啊?
回复

使用道具 举报

🔗
大Polo 2018-10-24 14:13:05 | 只看该作者
全局:
  1. import unittest

  2. class TrieNode:
  3.     def __init__(self):
  4.         self.table = dict()
  5.         self.phraseEnd = False


  6. class Trie:
  7.     def __init__(self):
  8.         self.root = TrieNode()

  9.     def insertPhrase(self, phrase):
  10.         node = self.root
  11.         words = phrase.split(' ')

  12.         for word in words:
  13.             if word not in node.table:
  14.                 node.table[word] = TrieNode()
  15.             node = node.table[word]

  16.         node.phraseEnd = True

  17.     def search(self, words):
  18.         node = self.root

  19.         for word in words:
  20.             if node.phraseEnd:
  21.                 return True
  22.             if word not in node.table:
  23.                 return False
  24.             node = node.table[word]
  25.         return node.phraseEnd


  26. class Solution:
  27.     def isSafe(self, blackList, pintext):
  28.         if not blackList or not pintext:
  29.             return True
  30.         
  31.         trie = Trie()
  32.         for phrase in blackList:
  33.             trie.insertPhrase(phrase)
  34.         
  35.         pintextWords = pintext.split(" ")
  36.         for i in range(len(pintextWords)):
  37.             if trie.search(pintextWords[i:]):
  38.                 return False
  39.         return True

  40. class SolutionTest(unittest.TestCase):
  41.     def testIsSafe(self):
  42.         sol = Solution()
  43.         blackList = ["machine guns", "world war i"]
  44.         self.assertFalse(sol.isSafe(blackList, "i love world war i"))
  45.         self.assertTrue(sol.isSafe(blackList, "world war ii"))

  46. if __name__=='__main__':
  47.     unittest.main()
复制代码

补充内容 (2018-10-24 14:13):
试着用Trie的思路做了第一个pintext safe的问题
回复

使用道具 举报

🔗
haruharu 2018-10-25 01:19:35 | 只看该作者
全局:
请问下lz 可以用白板写思路么,然后laptop coding。还想问下每轮有多少时间是在问简历或者bq的?谢谢!
回复

使用道具 举报

🔗
FML 2018-10-25 02:02:40 | 只看该作者
全局:
泡芙小姐的金鱼 发表于 2018-10-24 11:36
不会出现的,就是直接上级,一个多叉树

谢谢楼主,祝你offer多多
回复

使用道具 举报

🔗
 楼主| 泡芙小姐的金鱼 2018-10-25 13:24:12 | 只看该作者
全局:
金妮韦崽 发表于 2018-10-24 13:19
楼主是网申还是怎样的啊?

朋友内推的~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表