当前位置:网站首页>LeetCode_ 2341_ How many pairs can an array form
LeetCode_ 2341_ How many pairs can an array form
2022-07-23 13:50:00 【Fitz1318】
Topic link
Title Description
I'll give you a subscript from 0 The starting array of integers nums . In one step , You can do the following :
- from
numselect Two equal Integers - from
numsRemove these two integers , To form a Number pair
Please come in nums Perform this operation several times on until it cannot be continued .
Returns a subscript from 0 Start 、 The length is 2 Array of integers for answer As the answer , among answer[0] Is the number of pairs formed ,answer[1] It's right nums Try to count the number of integers left after the above operation .
Example 1:
Input :nums = [1,3,2,1,3,2,2]
Output :[3,1]
explain :
nums[0] and nums[3] Form a number pair , And from nums Remove ,nums = [3,2,3,2,2] .
nums[0] and nums[2] Form a number pair , And from nums Remove ,nums = [2,2,2] .
nums[0] and nums[1] Form a number pair , And from nums Remove ,nums = [2] .
Cannot form more pairs . A total of 3 Pairs of numbers ,nums The rest of the world is 1 A digital .
Example 2:
Input :nums = [1,1]
Output :[1,0]
explain :nums[0] and nums[1] Form a number pair , And from nums Remove ,nums = [] .
Cannot form more pairs . A total of 1 Pairs of numbers ,nums The rest of the world is 0 A digital .
Example 3:
Input :nums = [0]
Output :[0,1]
explain : Cannot form a number of pairs ,nums The rest of the world is 1 A digital .
Tips :
1 <= nums.length <= 1000 <= nums[i] <= 100
Their thinking
Use one HashSet To traverse the array , For an element , If HashSet Contains this element , Then add one to the number of pairs , And then we can change this element from HashSet Remove , The last remaining number after several pairs of operations
AC Code
class Solution {
public int[] numberOfPairs(int[] nums) {
int num = 0;
HashSet<Integer> hashSet = new HashSet<>();
for (int i : nums) {
if (hashSet.contains(i)) {
hashSet.remove(i);
num++;
} else {
hashSet.add(i);
}
}
return new int[]{
num, nums.length - 2 * num};
}
}
边栏推荐
- These five points should be considered in the production of enterprise science and technology exhibition hall
- Kept dual machine hot standby
- LeetCode_47_全排列Ⅱ
- 腾讯MMKV的原理与实现
- C#做一个简单浏览器
- 内存取证之NSSCTF-OtterCTF 2018(复现赛)
- 数据库系统原理与应用教程(044)—— MySQL 查询(六):使用 LIMIT 选项实现分页查询
- SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
- Probability meditation: 2. The quantitative rules
- 数据库系统原理与应用教程(049)—— MySQL 查询(十一):子查询
猜你喜欢

Kept dual machine hot standby

欧洲“气荒”对中国有哪些影响?

QT creator.Pro file adds the corresponding library according to the kit

Method of entering mathematical formula into mark down document

如何保证消息的可靠传输?如果消息丢了会怎么办

prometheus+node-exporter+grafana 监控服务器系统资源

微服务重点

Talking about the CPU type of anroid device and the placement directory of so files

docker redis
![[图形学]ASTC纹理压缩格式](/img/d1/734fe91d56716c610980696a8e0d06.png)
[图形学]ASTC纹理压缩格式
随机推荐
CSDN recommended template
Interviewer: have you learned about the underlying implementation of reentrantlock? tell us your opinion
CenterNet目标检测模型及CenterFusion融合目标检测模型
Database view detailed exploration
【JS高级】正则入门基础—关于你想知道的正则表达式_01
图形管线(一)后处理阶段 alpha测试 模版测试 深度测试 混合
Optimising a 3D convolutional neural network for head and neck computed tomography segmentation with
Optimising a 3D convolutional neural network for head and neck computed tomography segmentation with
LeetCode_2341_数组能形成多少数对
数据库系统原理与应用教程(052)—— MySQL 的数据完整性(十四):交叉表查询(行列转换)
挖财开户风险性大吗,安全吗?
关于this指针
2022暑假软件创新实验室集训 项目实战1
高中语文教资考试大纲
Unity制作简单拦截近防炮——如何预测打击目标
SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
Ros2 self study notes: URDF robot modeling
C#:in、out、ref关键字
Tutorial on principles and applications of database system (040) -- MySQL query (II): set the column name or expression to be queried
Detailed explanation of decision tree