题目要求

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. 【Android】Android设计准则

    准则 下面的这些设计准则是为了让Android的用户体验团队保持用户最佳的体验而发明设计的. 把他们融合到你的创造力中,作为你的设计理念,而不是有意地去使用. 吸引我 用惊奇的方式来取悦我 一个漂亮的 ...

  2. rtrim() 函数 从字符串的末端开始删除空白字符!

    例子 在本例中,我们将使用 rtrim() 函数从字符串右端删除字符: <?php $str = "Hello World!\n\n"; echo $str; echo rt ...

  3. 【iCore4 双核心板_ARM】例程二十一:LWIP_TCP_SERVER实验——以太网数据传输

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  4. 【转】WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要是对文本输入控件进行样式开发,及相关扩展功能开发,主要内容包括: 基本文 ...

  5. Linux系统排查4——网络篇

    用于排查Linux系统的网络故障. 网络排查一般是有一定的思路和顺序的,其实排查的思路就是根据具体的问题逐段排除故障可能发生的地方,最终确定问题. 所以首先要问一问,网络问题是什么,是不通,还是慢? ...

  6. 【GIS】Vue esri-leaflet

    1.npm install esri-leaflet --save 2.引入包 import Leaflet from "leaflet"; var esri = require( ...

  7. Markdown 字体

    在 Markdown 中,使用 * 来表示斜体,使用 ** 来表示加粗,使用 <font> 标签来设置字体 .字号与颜色 *我是斜体* **我是粗体** <font face=&qu ...

  8. linux udev学习

    自2.6 核心开始,就可以使用udev 协助管理系统中各设备名称.例如,磁盘设备排序.网卡设备排序等.udev能动态地在/dev 目录里产生自定义的.标识性强的设备文件或设备链接.本文即以红旗Asia ...

  9. vs2017更新后web项目部分后台代码类没有颜色,也没有自动提示输入功能

    vs2017有的版本更新后默认.net framework框架是.net framework4.6.1,将项目的.net framework框架更改为4.6.1,颜色和自动提示出现

  10. Configure GenieACS

    GenieACS General Config config.json acts as the main configuration file and is stored in /path_to_ge ...