博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
888. Uncommon Words from Two Sentences
阅读量:6991 次
发布时间:2019-06-27

本文共 1363 字,大约阅读时间需要 4 分钟。

题目描述:

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.

 

Example 1:

Input: A = "this apple is sweet", B = "this apple is sour"Output: ["sweet","sour"]

Example 2:

Input: A = "apple apple", B = "banana"Output: ["banana"]

 

Note:

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. A and B both contain only spaces and lowercase letters.

解题思路:

使用unordered_map<string,int>记录每个单词出现的次数,次数为一的单词加入返回的vector。

代码:

1 class Solution { 2 public: 3     vector
uncommonFromSentences(string A, string B) { 4 istringstream sin1(A); 5 istringstream sin2(B); 6 string word; 7 unordered_map
words; 8 vector
res; 9 while (sin1>>word) {10 words[word]++;11 }12 while (sin2>>word) {13 words[word]++;14 }15 for (auto iter = words.begin(); iter != words.end(); ++iter) {16 if (iter->second == 1) {17 res.push_back(iter->first);18 }19 }20 return res;21 }22 };

 

转载于:https://www.cnblogs.com/gsz-/p/9463306.html

你可能感兴趣的文章
lombok使用方法
查看>>
多线程基础
查看>>
1028: C语言程序设计教程(第三版)课后习题8.2
查看>>
批量更新软连接脚本
查看>>
Linux 文件和目录的属性
查看>>
Log4j配置使用
查看>>
初步认识Hadoop
查看>>
jQuery对象扩展方法(Extend)深度解析
查看>>
9道前端技能编程题
查看>>
NOIP 2000年提高组复赛 单词接龙
查看>>
mysql-索引与优化
查看>>
sql server 2008安装需要一直重启。但重启后又没有达到效果。
查看>>
Psp个人软件开发工具
查看>>
uva 1395(kruskal变形)
查看>>
斜率优化
查看>>
php 比较运算符
查看>>
for循环效率问题求解答
查看>>
Android so lib库远程http下载和动态注册
查看>>
痞子衡嵌入式:并行接口NAND标准(ONFI)及SLC Raw NAND简介
查看>>
Q-criterion- definition and post-processing
查看>>