[抄题]:

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar.

For example, "great acting skills" and "fine drama talent" are similar, if the similar word pairs are pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relation is not transitive. For example, if "great" and "fine" are similar, and "fine" and "good" are similar, "great" and "good" are not necessarily similar.

However, similarity is symmetric. For example, "great" and "fine" being similar is the same as "fine" and "great" being similar.

Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs.

Finally, sentences can only be similar if they have the same number of words. So a sentence like words1 = ["great"] can never be similar to words2 = ["doubleplus","good"].

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

先存后取

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

可能有、可能没有value对应时,用 .getOrDefault 方法

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

在多维数组中,p[0] p [1]对应的还是一个元素,不是一坨元素

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

hashset 即使有多次对应关系,也只存一次

[关键模板化代码]:

[其他解法]:

[Follow Up]:

737. Sentence Similarity II union find真不懂为啥

[LC给出的题目变变变]:

[代码风格] :

在多维数组中,p[0] p [1]对应的还是一个元素,不是一坨元素,纠正一下概念。

class Solution {
public boolean areSentencesSimilar(String[] words1, String[] words2, String[][] pairs) {
//cc
if (words1.length != words2.length) {
return false;
} //ini, HahsMap<String, new HashSet<String>>
Map<String, HashSet<String>> map = new HashMap<>(); //put into hashmap
for (String p[] : pairs) {
if (!map.containsKey(p[0])) {
map.put(p[0], new HashSet<String>());
}
map.get(p[0]).add(p[1]);
} //check if a = b, ab, ba
for (int i = 0; i < words1.length; i++) {
if (!words1[i].equals(words2[i]) && !(map.getOrDefault(words1[i], new HashSet<String>())).contains(words2[i]) &&
!(map.getOrDefault(words2[i], new HashSet<String>())).contains(words1[i])) return false;
} return true;
}
}

734. Sentence Similarity 有字典数组的相似句子的更多相关文章

  1. [LeetCode] 734. Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  2. LeetCode 734. Sentence Similarity

    原题链接在这里:https://leetcode.com/problems/sentence-similarity/ 题目: Given two sentences words1, words2 (e ...

  3. 【LeetCode】734. Sentence Similarity 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...

  4. [LeetCode] 737. Sentence Similarity II 句子相似度 II

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  5. [LeetCode] Sentence Similarity II 句子相似度之二

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  6. [LeetCode] 737. Sentence Similarity II 句子相似度之二

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  7. JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray

    JSONModel 嵌套字典数组  JSONModel nest NSDictionary NSArray

  8. JS中遍历普通数组和字典数组的区别

    // 普通数组 var intArray = new Array(); intArray[0] = "第一个"; intArray[1] = "第二个"; fo ...

  9. iOS_字典数组 按key分组和排序

    int main(int argc, const charchar * argv[]) { @autoreleasepool { // 1.定义一个测试的字典数组 NSMutableArray *di ...

随机推荐

  1. MpVue开发之框架的搭建

    npm install --global  vue-cli vue脚手架 vue init mpvue/mpvue-quickstart  my-project 创建一个基于mpvue-quickst ...

  2. 【MFC】picture控件 两种有细微差别的动态加载图片方法

    摘自:http://www.jizhuomi.com/software/193.html VS2010/MFC编程入门之二十七(常用控件:图片控件Picture Control) 分类标签: 编程入门 ...

  3. Jolt Awards: The Best Books

    Jolt大奖素有“软件业界的奥斯卡”之美誉,共设通用类图书.技术类图书.语言和开发环境.框架库和组件.开发者网站等十余个分类,每个分类设有一个“震撼奖”(Jolt Award)和三个“生产力奖”(Pr ...

  4. poj 3517

    题目链接  http://poj.org/problem?id=3517 题意        约瑟夫环  要求最后删掉的那个人是谁: 方法        理解递推公式就行了  考虑这样一组数据  k ...

  5. Git 下载、安装与SSH配置

    一.Git学习笔记(基于Github) Git简介 Git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都 ...

  6. System.Web.HttpRequestValidationException: 从客户端(dbFlag=&quot;&lt;soap:Envelope xmlns...&quot;)中检测到有潜在危险的 Request.Form 值。

    System.Web.HttpRequestValidationException: 从客户端(dbFlag="<soap:Envelope xmlns...")中检测到有潜 ...

  7. 【spring源码学习】spring的IOC容器在初始化bean过程

    [一]初始化IOC的bean的时候Spring会执行的一些回调方法 (1)spring bean创建的前置处理 =>ApplicationContextAwareProcessor 在创建bea ...

  8. devops 几个方便的工具

    1. fake API      [canned](https://github.com/sideshowcoder/canned  )    fake API.      [wiremock](ht ...

  9. 【Xamarin】MonoTouch - iOS 使用 UIImagePickerController 打开图片库和相机选择图片修改头像

    Application tried to present modally an active controller <UIImagePickerController: 0x7b6ff400> ...

  10. Jquery3.x高版本支持IE8

    最近在做项目的时候,遇到一个安全漏洞的问题 检测到目标站点存在javascript框架库漏洞 解决办法是 将受影响的javascript框架库升级到最新版本. 好吧,就给你升吧,升完之后,我的天啊,尽 ...