当前位置:网站首页>LeetCode两句话中不常见单词
LeetCode两句话中不常见单词
2022-05-14 13:53:57【liyatjj】
两句话中不常见单词
句子 是一串由空格分隔的单词。每个 单词 仅由小写字母组成。
如果某个单词在其中一个句子中恰好出现一次,在另一个句子中却 没有出现 ,那么这个单词就是 不常见的 。
给你两个 句子 s1 和 s2 ,返回所有 不常用单词 的列表。返回列表中单词可以按 任意顺序 组织。
示例 1:
输入:s1 = “this apple is sweet”, s2 = “this apple is sour”
输出:[“sweet”,“sour”]
解读题目,其实就是找到两个句子当中出现一次的单词。
可以使用哈希表,键就是每个单词,值就是单词出现的个数。
首先要将两个字符串按空格进行分割,然后将单词加入到集合里面,之后进行遍历,将符合要求的加入到新建集合中,返回即可。
class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String,Integer> a = new HashMap<String,Integer>();
String[] arr = s1.split(" ");
for(String s:arr){
a.put(s,a.getOrDefault(s,0)+1);
}
String[] arr1 = s2.split(" ");
for(String s:arr1){
a.put(s,a.getOrDefault(s,0)+1);
}
List<String> ss = new ArrayList<String>();
for(Map.Entry<String,Integer> m: a.entrySet()){
if(m.getValue()==1){
ss.add(m.getKey());
}
}
return ss.toArray(new String[0]);
}
}
需要注意的是,Map中的getOrDefault(s,0),如果在遍历字符串过程中,发现已经有相应的key,就用key对应的值,如果没有,就使用默认的value值,默认为0,但是又在其后面加了1。于是,在将字符串加入到集合里面的时候,可以使用a.put(s,a.getOrDefault(s,0)+1);这样就能保证所有的键值均加入进去,而且值是1。
边栏推荐
- 逻辑回归(Logistic Regression)
- 【源码解析】StyleNeRF 之Train_encoder.py
- moderlarts第一次学习
- 为什么调用了BrandMapper里面的addBrand方法,这个方法已经给我一个返回值了,也在方法里面提交事务了,但是数据库还是没有增加数据
- 2022年5月7日刷题
- 2022.5.7-----leetcode.433
- 2022年5月7号博客内容SSM框架项目管理-------------超市管理系统(第十一课针对订单管理修改表中的数据表的数据)
- C#,扫雷游戏(Minesweeper)之壹——作弊手段大曝光
- 【开源库学习】OpenThreads
- 【OSG】Examples
猜你喜欢
随机推荐
- postman循环调用同一个接口
- (Transfer Learning and fine tuning)迁移学习与微调
- ICDAR 2021竞赛 科学文献分析——表格识别综述部分(剩余部分是文档布局分析)
- Webpach打包器的如何使用
- 2021-IEEE论文-深度神经网络在文档图像表格识别中的应用现状及性能分析
- vscode搭建go开发环境
- chrome 安装axure 插件
- P4 learning - Basic forwarding
- The contents of the input box are displayed on the right
- Clickhouse 22.3 lts release
- Programmer flirting special ~ ~ ~ nice H5 cube creative photo album, resources free!!! A gift from a programmer to a girl is very suitable for a young lady!
- [missing scan tool] awvs, appscan download and installation (with network disk link)
- paramiko下载大文件出错问题 sftp
- 5.3 binary tree_ Code implementation of optimized heap and Top-k problem
- 5.4 binary tree_ Code implementation of various traversal and calculation
- Record: com mysql. cj. jdbc. exceptions. CommunicationsException: Communications link failure... [effective through personal test]
- Record: 1221 - incorrect usage of Union and order by [effective through personal test]
- CLIP学习笔记
- [force deduction] backtracking 1 - Foundation + combination
- What role does cloud computing play in building intelligence?
- Abstract - the shortest novel of 2016
- Fiddler packet capture guide 05: breaking points
- SAP:SWITCH用法
- AD7606/AD7616使ZYNQ在能源电力领域如虎添翼,可实现16/32/64通道AD同步采样
- AM57x 多核SoC开发板——GPMC的多通道AD采集综合案例手册(上)
- 【日常训练】面试题 01.05. 一次编辑
- 【日常训练】384. 打乱数组
- AM57x 多核SoC开发板——GPMC的多通道AD采集综合案例手册(下)
- EDA technology and market analysis
- libmodus源码解读
- 一招win7 c盘瘦身
- Day 1:轮转数组
- 美团四大名著为什么不是三或五
- 是能力更是文化,談談IT系統的安全發布
- C'est la capacité, c'est la culture.
- Am57x multi-core SoC development board -- GPMC's comprehensive case manual for multi-channel AD acquisition (Part 2)
- [daily training] 384 Scramble array
- [daily training] interview question 01.05 One edit
- Am57x multi-core SoC development board -- GPMC's comprehensive case manual for multi-channel AD acquisition (Part I)
- Ad7606 / ad7616 make zynq more powerful in the field of energy and power, and can realize 16 / 32 / 64 channel ad synchronous sampling