当前位置:网站首页>LeetCode 2341. 数组能形成多少数对
LeetCode 2341. 数组能形成多少数对
2022-07-27 23:22:00 【Michael阿明】
1. 题目
给你一个下标从 0 开始的整数数组 nums 。在一步操作中,你可以执行以下步骤:
- 从 nums 选出 两个 相等的 整数
- 从 nums 中移除这两个整数,形成一个 数对
请你在 nums 上多次执行此操作直到无法继续执行。
返回一个下标从 0 开始、长度为 2 的整数数组 answer 作为答案,其中 answer[0] 是形成的数对数目,answer[1] 是对 nums 尽可能执行上述操作后剩下的整数数目。
示例 1:
输入:nums = [1,3,2,1,3,2,2]
输出:[3,1]
解释:
nums[0] 和 nums[3] 形成一个数对,并从 nums 中移除,nums = [3,2,3,2,2] 。
nums[0] 和 nums[2] 形成一个数对,并从 nums 中移除,nums = [2,2,2] 。
nums[0] 和 nums[1] 形成一个数对,并从 nums 中移除,nums = [2] 。
无法形成更多数对。总共形成 3 个数对,nums 中剩下 1 个数字。
示例 2:
输入:nums = [1,1]
输出:[1,0]
解释:nums[0] 和 nums[1] 形成一个数对,并从 nums 中移除,nums = [] 。
无法形成更多数对。总共形成 1 个数对,nums 中剩下 0 个数字。
示例 3:
输入:nums = [0]
输出:[0,1]
解释:无法形成数对,nums 中剩下 1 个数字。
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 100
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-number-of-pairs-in-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- Counter 计数,把个数拿出来整除2
from collections import Counter
class Solution:
def numberOfPairs(self, nums: List[int]) -> List[int]:
c = Counter(nums).values()
ct = sum([x//2 for x in c])
return [ct, len(nums)-ct*2]
28 ms 15 MB Python3
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
边栏推荐
- At the meeting on June 19, SMIC may set the fastest listing record in China!
- C语言main函数传递参数
- 站在数字零售转型的十字路口,我们需要用新的角度来看待它
- Node red interacts with tdengine
- S-RPN: Sampling-balanced region proposal network for small crop pest detection
- Spreadsheet export excel table
- 2022/07/27 学习笔记 (day17) 代码块和内部类
- 重新定义分析 - EventBridge 实时事件分析平台发布
- 华米科技“黄山2号”发布:AI性能提升7倍,功耗降低50%!
- From functional testing to automated testing, my monthly salary has exceeded 30k+, and I have 6 years of testing experience.
猜你喜欢

字节月薪28K,分享一波我的自动化测试经验....

2022/07/27 learning notes (Day17) code blocks and internal classes

Lua get started quickly

Count the number of given strings in a string

Byte monthly salary 28K, share a wave of my automation testing experience

测试人员需要了解的软件流程

Summary of common shortcut keys in idea

Learn how Baidu PaddlePaddle easydl realizes automatic animal recognition in aquarium

Principle of logistic regression

Matlab drawing - points and vectors: method and source code of vector addition and subtraction
随机推荐
Count the number of given strings in a string
深圳华强宣布拟不超2000万元入股比亚迪半导体
20 bad habits of bad programmers
Introduction to the browser video frame operation method requestvideoframecallback()
Fluent call interface UI
Adding custom dynamic arts and Sciences to cesium
The total investment is nearly 1.6 billion yuan! Qianzhao optoelectronics VCSEL and high-end LED chip projects officially started
国产NB-IoT芯片厂商芯翼信息科技获2亿元A+轮融资
在一个字符串里面统计给定字符串的个数
BigDecimal common API
对迁移学习中域适应的理解和3种技术的介绍
C language main function transfer parameters
BSP video tutorial issue 21: easy one key implementation of serial port DMA variable length transceiver, support bare metal and RTOS, including MDK and IAR, which is more convenient than stm32cubemx (
吴雄昂发内部信:Arm的指控是莫须有的,现有成果不允许有任何损害!
Swoole websocket service
Redis cache penetration breakdown and avalanche
Lua进阶
美光起诉联电窃密案宣判:联电被罚1亿元新台币,三名员工被判刑!
Day 013 one dimensional array exercise
2022/07/27 学习笔记 (day17) 代码块和内部类