网站开发与应用 论文,asp 课程教学网站开发,宁波优化网站排名价格表,如何制作短视频力扣题
1、题目地址
2199. 找到每篇文章的主题
2、模拟表
表#xff1a;Keywords
Column NameTypetopic_idintwordvarchar
(topic_id, word) 是该表的主键#xff08;具有唯一值的列的组合#xff09;。该表的每一行都包含一个主题的 id 和一个用于表达该主题的词。可…力扣题
1、题目地址
2199. 找到每篇文章的主题
2、模拟表
表Keywords
Column NameTypetopic_idintwordvarchar
(topic_id, word) 是该表的主键具有唯一值的列的组合。该表的每一行都包含一个主题的 id 和一个用于表达该主题的词。可以用多个词来表达同一个主题也可以用一个词来表达多个主题。
表Posts
Column NameTypepost_idintcontentvarchar
post_id 是该表的主键具有唯一值的列。该表的每一行都包含一个帖子的 ID 及其内容。内容仅由英文字母和空格组成。
3、要求
Leetcode 从其社交媒体网站上收集了一些帖子并对每个帖子的主题感兴趣。
每个主题可以由一个或多个关键字表示。
如果某个主题的关键字存在于一个帖子的内容中 (不区分大小写)那么这个帖子就有这个主题。
编写解决方案根据以下规则查找每篇文章的主题 1、如果帖子没有来自任何主题的关键词那么它的主题应该是 “Ambiguous!”。 2、如果该帖子至少有一个主题的关键字其主题应该是其主题的 id 按升序排列并以逗号 ‘’ 分隔的字符串。字符串不应该包含重复的 id。 以 任意顺序 返回结果表。
4、示例
输入
Keywords 表
topic_idword1handball1football3WAR2Vaccine
Posts 表
post_idcontent1We call it soccer They call it football hahaha2Americans prefer basketball while Europeans love handball and football3stop the war and play handball4warning I planted some flowers this morning and then got vaccinated
输出
post_idtopic112131,34Ambiguous!
解释
1“We call it soccer They call it football hahaha” “football” 表示主题 1。没有其他词能表示任何其他主题。
2“Americans prefer basketball while Europeans love handball and football” “handball” 表示主题 1。“football” 表示主题 1。 没有其他词能表示任何其他主题。
3“stop the war and play handball” “war” 表示主题 3。 “handball” 表示主题 1。 没有其他词能表示任何其他主题。
4“warning I planted some flowers this morning and then got vaccinated” 这个句子里没有一个词能表示任何主题。注意 “warning” 和 “war” 不同尽管它们有一个共同的前缀。 所以这篇文章 “Ambiguous!” 请注意可以使用一个词来表达多个主题。
5、代码编写
知识点
group_concat 用法可参考我以前文章
【MySQL】CONCAT、CONCAT_WS、GROUP_CONCAT 函数用法
字符串函数 locate 用法
语法locate(substr,str) 作用用于返回 str 中 substr 所在的位置索引如果找到了则返回一个大于0的数否则返回0。 例子比如在 table 表里有个字段名 field 值为 “I like playing”如果要将这个字段包含“like”的查询出来可以用 select * from table where locate(like, field) 0
我的代码
里面比较关键的一点是需要对匹配的字符串和被匹配的字符串前后都加空格原因 1、对匹配的字符串前后加空格防止错误匹配war - warning 2、对被匹配的字符串前后加空格防止前后匹配不到’ handball ’ - handball
select one.post_id, ifnull(group_concat(distinct topic_id order by topic_id separator ,), Ambiguous!) AS topic
from Posts one
left join Keywords two on locate(concat( , two.word, ), concat( , one.content, )) 0
group by 1