题目要求

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. java转换日期格式为 RFC1123

    import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util. ...

  2. Node入门教程(10)第八章:Node 的事件处理

    Node中大量运用了事件回调,所以Node对事件做了单独的封装.所有能触发事件的对象都是 EventEmitter 类的实例,所以上一篇我们提到的文件操作的可读流.可写流等都是继承了 EventEmi ...

  3. 【iCore4 双核心板_ARM】例程十:RTC实时时钟实验——显示时间和日期

    实验现象: 核心代码: int main(void) { /* USER CODE BEGIN 1 */ RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; ; ...

  4. 关于Unity中NGUI的3D角色血条的实现

    首先要到Unity的Assets Store里面去下载一个扩展的Package叫NGUI HUD Text v1.13(81),注意如果没有安装NGUI就必须先安装NGUI插件,否则会用不了,因为HU ...

  5. hosts 不启作用的解决办法

    查各种编码ANSI flushdns无用 停止DNS Client  服务  0.0

  6. Xcode6:模拟器消失了?

    今天打开Xcode,选择模拟器时发现只剩下了“iPhone 5”和“iPhone 5s”,原来什么“iPad Air”,“iPhone 4s”的都哪里去了?丢了? 别着急,依次打开“Xcode-> ...

  7. myeclipse创建hibernate工程

    1.创建数据库: from blog http://www.cnblogs.com/zhaocundang/p/9061959.html 使用navicat mysql IDE: 创建数据库 book ...

  8. css背景色半透明的最佳实践

    之前项目中遇到纯色的半透明背景,都是这么干: <style> .box {width:300px;height:300px;position:relative;} .mask {width ...

  9. sqlzoo需要知道的那些事

    1.“Bahamas 巴哈馬”中有三個 a,還有嗎?找出所有國家,其名字包括三個或以上的a. SELECT name FROM world WHERE name LIKE '%a%a%a%' 2.“I ...

  10. MySql表结构修改详解

    修改表的语法=========================增加列[add 列名]=========================①alter table 表名 add 列名 列类型 列参数[加的 ...