题目要求

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. 用Jmeter+Badboy+Fiddler做接口测试

    用Jmeter+Badboy+Fiddler做接口测试 2016-12-05 目录: 1 简介2 Badboy录制3 Jmeter打开Badboy脚本4 用Fiddler抓请求,补充完善脚本5 测试中 ...

  2. java 实现websocket

    最近了解了下websocket和socket这个东西,说不得不来说下为何要使用 WebSocket ,和为何不用http. 为何需要WebSocket ? HTTP 协议是一种无状态的.无连接的.单向 ...

  3. 如何进行 iPhone 客户端的软件测试

    如何进行 iPhone 客户端的软件测试客户端版APP主要是通过苹果的APP Store来进行安装的.在测试时,开发会先在本地苹果机上打好包,然后我们在Xcode上进行安装或者直接在开发提供的网址上下 ...

  4. js 六种数据类型的区别及bool 转换判断

    一.bool型转换判断: 1.true 和 1 比较是相同,false 和 0 比较是相同(是 “==” 比较),因为内部会实现数据类型的 转化,将true 转换成1,将false 转换成0, js ...

  5. ThinkingInJava 学习 之 0000004 初始化与清理

    1. 用构造器确保初始化. 不接受任何参数的构造器叫做默认构造器. Tree tree = new Tree(12); 如果Tree(int)时Tree类的唯一的构造器,那么编译器将不会允许你以其他任 ...

  6. 使用 wondershaper 在 Linux 中限制网络带宽使用

    wondershaper 实际上是一个 shell 脚本,它使用 tc 来定义流量调整命令,使用 QoS 来处理特定的网络接口.外发流量通过放在不同优先级的队列中,达到限制传出流量速率的目的:而传入流 ...

  7. Linux内核之旅

    http://www.kerneltravel.net/ Linux内核之旅 Linux Kernel Travel

  8. iOS mac终端下的SQL语句

    本文转载至 http://blog.csdn.net/majiakun1/article/details/41281801 我们都知道数据库的创建可以借助图形化的数据库工具软件,但也可以在Mac终端下 ...

  9. c++ union内存

    看一个例子: #include <iostream> #include <stdio.h> using namespace std; union A { int a; stru ...

  10. 【本周面试题】第1周 - 获取URL中的查询字符串参数、get和post的区别

    [此系列优先解决自己经历的面试题] 2018.11.16 面试题一:你如何获取浏览器URL中查询字符串中的参数? 题目代码: 测试地址为 https://www.sogou.com/tx?query= ...