当前位置:网站首页>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阿明),一起加油、一起学习进步!
边栏推荐
- The total investment is nearly 1.6 billion yuan! Qianzhao optoelectronics VCSEL and high-end LED chip projects officially started
- Neuron 2.1.0 release: it supports sparkplug B specification and more complete industrial protocol support
- Support Wu xiongang! Arm Chinese management sent an open letter: shocked and angry at the unwarranted allegations!
- Introduction to the browser video frame operation method requestvideoframecallback()
- 20 bad habits of bad programmers
- JS global function method module exports exports
- Data problems can also be found if there is a space at the end of the field value of MySQL query criteria
- Adding custom dynamic arts and Sciences to cesium
- Lua进阶
- Shenzhen Huaqiang announced that it plans to invest no more than 20million yuan in BYD semiconductor
猜你喜欢

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 (

How the test architects of bat factories interpret various disputes of the test platform

彻底搞懂kubernetes调度框架与插件

Principle of logistic regression

迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码

“蔚来杯“2022牛客暑期多校训练营3 补题题解(A、C、J)

Thoroughly understand kubernetes scheduling framework and plug-ins

Gossip: an initially perfect FS is as impractical as the first version of the program requiring no bugs

杂谈:一份最初就非常完善的FS跟第一版程序就要求没bug一样不切实际

Redis sentinel mode
随机推荐
测试人员需要了解的软件流程
Wentai technology acquired the remaining equity of ANSYS semiconductor and obtained unconditional approval
Summary of common shortcut keys in idea
Redis cache penetration breakdown and avalanche
Byte monthly salary 28K, share a wave of my automation testing experience
mysql-JPA对数据库中JSON类型数据的支持
Arm中国夺权大战的背后:“独立”两年,仍难“自主”?
6月19日上会,中芯国际或创造国内最快上市记录!
糟糕程序员的20个坏习惯
华为回应美国封锁供应链:他们仍需为5G专利付费
Basic concept and classification of i/o equipment
吴雄昂发内部信:Arm的指控是莫须有的,现有成果不允许有任何损害!
URDF integrated gazebo
Swoole collaboration
如何让数字零售承接起流量时代和留量时代的发展重任,或许才是关键所在
Kibana6.2.4 version update x-pack certification
闻泰科技收购安世半导体剩余股权获得无条件通过
Cross domain requests in nodejs
Huawei responded to the US blockade of the supply chain: they still have to pay for 5g patents
Codeforces暑期训练周报(7.14~7.20)