题目要求

We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word consists only of lowercase letters.)

A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.

Return a list of all uncommon words.

You may return the list in any order.

题目分析及思路

给定两个句子,每个句子是一个由空格隔开的词组成的字符串,每个词只含小写字母。一个词是uncommon的条件是它仅在一个句子中出现一次。最后返回所有的uncommon的词。可以建一个dict,将词作为key,出现的次数为value。最后uncommon的词即为value为1的词。

python代码

class Solution:

def uncommonFromSentences(self, A: 'str', B: 'str') -> 'List[str]':

a = A.split()

b = B.split()

c = {}

a.extend(b)

for e in a:

if e not in c:

c[e] = 1

else:

c[e] += 1

return [key for key, value in c.items() if value == 1]

LeetCode 884 Uncommon Words from Two Sentences 解题报告的更多相关文章

  1. 【LeetCode】884. Uncommon Words from Two Sentences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetc ...

  2. [LeetCode] 884. Uncommon Words from Two Sentences 两个句子中不相同的单词

    We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word co ...

  3. LeetCode 884. Uncommon Words from Two Sentences (两句话中的不常见单词)

    题目标签:HashMap 题目给了我们两个句子,让我们找出不常见单词,只出现过一次的单词就是不常见单词. 把A 和 B 里的word 都存入 map,记录它们出现的次数.之后遍历map,把只出现过一次 ...

  4. 【Leetcode_easy】884. Uncommon Words from Two Sentences

    problem 884. Uncommon Words from Two Sentences 题意:只要在两个句子中单词出现总次数大于1次即可. 注意掌握istringstream/map/set的使 ...

  5. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  6. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  7. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

随机推荐

  1. 搭建redis集群

    官方详细介绍请移步:http://www.redis.cn/topics/cluster-tutorial.html 这里总结性给出搭建步骤: 1. 至少6个节点,三主三从 2. 编译redis源码 ...

  2. 使用apktool.jar工具反编译和回编译Android APK 终端命令模式

    1.工具准备 工具可以网上搜索下载新版本,也可以从这里Download:https://github.com/FlymeOS/tools/blob/lollipop-5.1/reverses/apkt ...

  3. hdoj:2023

    #include <iostream> #include <string> #include <vector> ][],b[],c[]; using namespa ...

  4. azkaban在centos下的部署安装

    azkaban 是一个用Java开发的开源调度工具workflow. 下面介绍具体安装过程,我这里使用的版本是3.43.0,使用的是solo运行模式. 编译 git clone https://git ...

  5. ABBYY PDF Transformer+系统要求

    ABBYY PDF Transformer+是一个新的,全面的巧妙解决PDF文档的工具,它将泰比的光学字符识别(OCR)技术和Adobe®PDF技术完美结合,以确保实现便捷地处理任何类型的PDF文件, ...

  6. DedeCMS织梦文章页图片地址为绝对路径实现方法

    {dede:field.body function='replaceurl(@me)'/} 余斗博客改版后增加了一个m站点即手机站点,用二级域名实现,在做手机站的过程中发现一个问题,手机站和pc站都是 ...

  7. duilib进阶教程 -- 在MFC中使用duilib (1)

    由于入门教程的反响还不错,因此Alberl就以直播的形式来写<进阶教程>啦,本教程的前提: 1.请先阅读<仿迅雷播放器教程> 2.要有一定的duilib基础,如果还没,请先阅读 ...

  8. js函数作用域

    函数 1.函数没有用return返回函数时,返回默认参数undefined 结果 return返回得话 就是里面得数值 结果 JS执行过程是上到下,下面的a元素覆盖了上面的a元素 function d ...

  9. YAML入门

    概要 YAML(是YAML Ain't Markup Language的缩写,尾音的发音类似Camel)是一种序列化数据的语言(类似json, xml),使用轻量高可读性的语法描述list, dict ...

  10. day_6.16网络编程

    单线程服务器select版: select ---->最多1024个 poll ----->解决了套接字上限的问题----->轮询检测 关于 sys模块: 通过fd找套接字 协程: ...