734. Sentence Similarity 有字典数组的相似句子
[抄题]:
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 有字典数组的相似句子的更多相关文章
- [LeetCode] 734. Sentence Similarity 句子相似度
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- LeetCode 734. Sentence Similarity
原题链接在这里:https://leetcode.com/problems/sentence-similarity/ 题目: Given two sentences words1, words2 (e ...
- 【LeetCode】734. Sentence Similarity 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...
- [LeetCode] 737. Sentence Similarity II 句子相似度 II
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] 737. Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray
JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray
- JS中遍历普通数组和字典数组的区别
// 普通数组 var intArray = new Array(); intArray[0] = "第一个"; intArray[1] = "第二个"; fo ...
- iOS_字典数组 按key分组和排序
int main(int argc, const charchar * argv[]) { @autoreleasepool { // 1.定义一个测试的字典数组 NSMutableArray *di ...
随机推荐
- 【django】Bootstrap 安装和使用
官网 下载:推荐下载源码包 安装Bower:使用Bower安装并管理 Bootstrap 的Less.CSS.JavaScript和字体文件(通过npm安装bower) npm install -g ...
- JAVA软件配置—环境变量
环境Windows10,JDK,JRE1.8.0_102 鼠标右击左下角Windows图标,选择"系统"项: 点击"高级系统设置"——"环境变量&qu ...
- python笔记-4(装饰器、生成器、迭代器)
一.熟练掌握装饰器的原理 (在装饰器学习的过程中,查了看了很多资料,个人感觉走了很多的弯路,这个笔记,分享我的理解,希望能帮助到一些人.本文对装饰器的描述,侧重点是条理与逻辑思路,想通过从无到有的方式 ...
- 完整的验证码识别流程基于svm(若是想提升,可优化)
字符型图片验证码识别完整过程及Python实现 首先很感觉这篇文章的作者,将这篇文章写的这么好.我呢,也是拿来学习,觉得太好,所以忍不住就进行了转载. 因为我个人现在手上也有个验证码识别的项目,只是难 ...
- raw_in_fields
在admin后台类中加入raw_id_fields(只适用于外键)后,会显示外键的详细信息
- xlrd,xlwt读表格、写表格
转:http://www.jb51.net/article/60510.htm
- Python 中,字符串"连接"效率最高的方式是?一定出乎你的意料
网上很多文章人云亦云,字符串连接应该使用「join」方法而不要用「+」操作.说前者效率更高,它以更少的代价创建新字符串,如果用「+」连接多个字符串,每连接一次,就要为字符串分配一次内存,效率显得有点低 ...
- php sprintf()
在写php代码的时候,有时候会用到sprintf()这个函数,那么它是怎么用的呢? 学习源头: http://www.w3school.com.cn/php/func_string_sprintf.a ...
- 谷歌浏览器无法播放QQ空间视频动画的解决方案
https://qzonestyle.gtimg.cn/qzone/photo/v7/js/module/flashDetector/flash_tutorial.pdf Chrome开启⽅法 1. ...
- redirect uri 参数错误 怎么办
这种情况,多数是因为请求地址不合法所致. 去公众号中添加合法的地址. 这种地址需要满足一些条件. 设置地址 满足的条件 保证可以访问到安全文件 如果访问不到的话,将无法保存 这里是文件存放位置 经过这 ...