12
返回列表 发新帖
楼主: Boba
跳转到指定楼层
上一主题 下一主题
收起左侧

狗家店面

🔗
idatascience 2018-4-28 05:02:49 | 只看该作者
全局:
shuaiwa 发表于 2018-4-28 04:54
乐扣原题还是简单版的, follow up是加传递性吧

求leetcode题号~大神~
回复

使用道具 举报

🔗
idatascience 2018-4-28 05:04:08 | 只看该作者
全局:
shufezq 发表于 2018-4-28 04:59
一段时间没刷题了,leetcode是不是有个同义词的题目是这个意思? 用hashmap加dfs或者union find可解,lz的b ...

这个题楼主好像没有说可以传递,如果不可以传递,union find就不适用了吧~
回复

使用道具 举报

🔗
idatascience 2018-4-28 05:22:04 | 只看该作者
全局:
假设有传递性:
class Solution:
    def isSynonym(self, syn, queries):

        parentWord = {}

        for i, s in enumerate(syn):
            self.union(s[0], s[1], parentWord)

        res = []

        for query1, query2 in queries:
            wordList1 = query1.split()
            wordList2 = query2.split()
            if len(wordList1) != len(wordList2):
                res.append(False)
                continue
            flag = 0
            for word1, word2 in zip(wordList1, wordList2):
                if self.find(word1, parentWord) != self.find(word2, parentWord):
                    res.append(False)
                    flag = 1
                    break

            if not flag:
                res.append(True)

        return res

    def find(self, word, parentWord):
        if word not in parentWord:
            parentWord[word] = word
            return word
        tmp = word
        while parentWord[word] != word:
            word = parentWord[word]
        parentWord[tmp] = word
        return word

    def union(self, word1, word2, parentWord):
        pword1 = self.find(word1, parentWord)
        pword2 = self.find(word2, parentWord)
        parentWord[pword1] = pword2
回复

使用道具 举报

全局:
面试官给提示吗?
回复

使用道具 举报

🔗
 楼主| Boba 2018-4-28 06:25:52 | 只看该作者
全局:

嗯嗯,不懂就问
回复

使用道具 举报

🔗
 楼主| Boba 2018-4-28 06:28:35 | 只看该作者
全局:
idatascience 发表于 2018-4-28 04:58
请问楼主follow up是什么呀?

因为我的做法如果有重复的词在dictionary会有些问题所以要我解决那个问题。当时真的紧张脑袋一片空白了
回复

使用道具 举报

🔗
kevinxw 2018-4-28 07:08:39 | 只看该作者
全局:
这个不是union find的问题么?
可以去搜搜利口 sentence similarity
回复

使用道具 举报

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

本版积分规则

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