【LeetCode】734. Sentence Similarity 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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”].
Note:
- The length of words1 and words2 will not exceed 1000.
- The length of pairs will not exceed 2000.
- The length of each
pairs[i]
will be 2. - The length of each
words[i]
andpairs[i][j]
will be in the range[1, 20]
.
题目大意
给定两个句子 words1, words2 (每个用字符串数组表示),和一个相似单词对的列表 pairs ,判断是否两个句子是相似的。
解题方法
只修改区间起终点
这个题有一个坑的地方没说清楚:一个词可能会和多个词相似。
因此使用map<string, set>的方式,保存{每个词 : 与之相似的词}所有映射关系。
注意相似是双向的,因此对于一个Pair,需要正反插入两次。
判断是否相似的时候,需要对两个列表的对应元素进行遍历,判断是否相等或者在其相似set中出现。
C++代码如下:
class Solution {
public:
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<vector<string>>& pairs) {
if (words1.size() != words2.size()) return false;
const int N = words1.size();
unordered_map<string, unordered_set<string>> similar;
for (auto& pair : pairs) {
similar[pair[0]].insert(pair[1]);
similar[pair[1]].insert(pair[0]);
}
for (int i = 0; i < N; ++i) {
if (words1[i] != words2[i] && !similar[words1[i]].count(words2[i])) {
return false;
}
}
return true;
}
};
日期
2019 年 9 月 18 日 —— 今日又是九一八
【LeetCode】734. Sentence Similarity 解题报告(C++)的更多相关文章
- [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] 737. Sentence Similarity II 句子相似度 II
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- Linux服务器查看个人硬盘配额
quota -uvs
- 【基因组注释】GMAP安装使用问题
homology策略预测基因结构,下载了公共mRNA/CDS序列,考虑用gmap比对.本来是个很简单的脚本,但总是不那么顺利. 无论是用conda安装,还是源码安装较新版本,都存在问题. gmap_b ...
- shell 除法和格式化输出printf
相关知识的补充: printf命令模仿C程序库里的printf()程序.printf由POSIX标准所定义,因此使用printf的脚本比使用echo有着更好的移植性. printf使用引用文本或者空格 ...
- docker可视化管理Portainer
Portainer是一款轻量级docker容器管理平台,占用资源少,支持集群,支持权限分配. $ docker volume create portainer_data$ docker run -d ...
- javaSE高级篇5 — java8新特性详解———更新完毕
java8新特性 在前面已经见过一些东西了,但是:挖得有坑儿 1.lambda表达式 lambda表达式是jdk1.8引入的全新语法特性 它支持的是:只有单个抽象方法的函数式接口.什么意思? 就是说: ...
- 云原生时代的 APM
作者 | 刘浩杨 来源|尔达 Erda 公众号 APM 的全称是 Application Performance Management(应用性能管理),早在 90 年代中期就有厂商提出性能管理的概念 ...
- 关于learning Spark中文版翻译
在网上找了很久中文版,感觉都是需要支付一定金币才能下载,索性自己翻译算了.因为对Spark有一定了解,而且书籍前面写道,对Spark了解可以直接从第三章阅读,就直接从第三章开始翻译了,应该没有什么 ...
- Kafka(一)【概述、入门、架构原理】
目录 一.Kafka概述 1.1 定义 二.Kafka快速入门 2.1 安装部署 2.2 配置文件解析 2.3Kafka群起脚本 2.4 topic(增删改查) 2.5 生产和消费者命令行操作 三.K ...
- Android 极光推送集成
集成Jpush 1.用Android Studio创建一个Demo 2.创建激光推送开发者账号,要创建极光推送开发者帐号,请访问极光推送官方网站https://www.jiguang.cn/push ...
- Android Menu的基本用法
使用xml定义Menu 菜单资源文件必须放在res/menu目录中.菜单资源文件必须使用<menu>标签作为根节点.除了<menu>标签外,还有另外两个标签用于设置菜单项和分组 ...