[抄题]:

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. [qt][问题记录] 无法定位程序输入点 _ZdaPvj 于动态链接库 libstdc++-6.dll

    无法定位程序输入点 _ZdaPvj 于动态链接库 libstdc++-6.dll 该问题是没有打包库的问题,之所以出现这个问题的是直接用系统自带的命令行使用qt的windeployqt命令导致提供的库 ...

  2. SSH使用总结(xml配置)

    beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  3. 重温CLR(三)类型基础

    所有类型都从System.Object派生 “运行时”要求每个类型最终都要从System.Object类型派生.也就是说,一下两个类型的定义完全一致. //隐式派生自Object class Empl ...

  4. Protobuf-net 应用

    什么是ProtoBuf-net Protobuf是google开源的一个项目,是基于二进制的类似于XML,JSON这样的数据表示语言,用户数据序列化反序列化,google声称google的数据通信都是 ...

  5. apt安装工具

    apt-cache search package 搜索包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt-get install package ...

  6. Delphi使用Indy、ICS组件读取网页

    使用Indy 10中TIdHTTP的例子: 代码 uses IdHttp; . . . function HttpGet(const Url: string; var Html: string): B ...

  7. expected_conditions 库的使用方法

    from selenium.webdriver.support import expected_conditions as EC 例子一: 例子二:(判断元素存在文本"糯米")

  8. Java-Maven-Runoob:Maven 依赖管理

    ylbtech-Java-Maven-Runoob:Maven 依赖管理 1.返回顶部 1. Maven 依赖管理 Maven 一个核心的特性就是依赖管理.当我们处理多模块的项目(包含成百上千个模块或 ...

  9. php判断是否为ajax请求

    先说前端使用 jQuery 时怎么区分: jQuery 发出 ajax 请求时,会在请求头部添加一个名为 X-Requested-With 的信息,信息内容为:XMLHttpRequest 在后端可以 ...

  10. JAVA 比较两个日期相差的天数

    在实际的应用中,我们经常会比较两个日期相差的天数,下面我们通过java方法判断两个日期所差的额天数. 具体内容,请看下面的代码: package com.jd.jr.fclient.test; imp ...