当前位置:网站首页>Leetcode 2341. How many pairs can an array form
Leetcode 2341. How many pairs can an array form
2022-07-28 01:35:00 【Michael Amin】
List of articles
1. subject
I'll give you a subscript from 0 The starting array of integers nums . In one step , You can do the following :
- from nums elect Two equal Integers
- from nums Remove 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 <= 100
0 <= nums[i] <= 100
source : Power button (LeetCode)
link :https://leetcode.cn/problems/maximum-number-of-pairs-in-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Problem solving
- Counter Count , Take out the number and divide it 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
my CSDN Blog address https://michael.blog.csdn.net/
Long click or sweep code pay attention to my official account (Michael amin ), Come on together 、 Learn together !
边栏推荐
- 面试题 01.09. 字符串轮转
- 软件测试面试题:think_time的作用是什么?
- URDF integrated gazebo
- Flutter--密码登录注册界面
- 在一个字符串里面统计给定字符串的个数
- Can anime characters become "real people"? Paddegan helps you find the TA of "tear man"
- Meguiar sued liandian for secret theft and sentenced: liandian was fined NT $100million and three employees were sentenced!
- Arm中国夺权大战的背后:“独立”两年,仍难“自主”?
- Briefly understand namenode and datanode
- Cesium add dynamic pop-up
猜你喜欢

Icml2022 | online decision transformer

Baidu PaddlePaddle easydl: when AI enters the factory, "small bearing" can also turn "big industry"

Three instance

3年经验想拿20K,居然面了半个月都没拿到?

Count the number of given strings in a string

华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘

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

PHP利用某些函数bypass waf探讨

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

Codeforces summer training weekly (7.21~7.27)
随机推荐
Interpretation of new features | the restriction of MySQL 8.0 on gtid is lifted
面试题 01.09. 字符串轮转
Lua快速上手
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 (
Three basic teaching
Byte monthly salary 28K, share a wave of my automation testing experience
How to make digital retail undertake the development task of the era of traffic and retention may be the key
Realize ABCD letter increment
3年经验想拿20K,居然面了半个月都没拿到?
软件测试面试题:如何发现数据库的相关问题?
Unity Shader入门精要学习——基础纹理
4月全球智能手机出货同比下滑41%,华为首次超三星成全球第一
在一个字符串里面统计给定字符串的个数
spreadsheet 导出 excel表格
华为旗下哈勃投资入股VCSEL芯片厂商纵慧芯光
闻泰科技收购安世半导体剩余股权获得无条件通过
晶方科技:光刻机厂商ASML为公司参与并购的Anteryon公司的最主要客户之一
Learn how Baidu PaddlePaddle easydl realizes automatic animal recognition in aquarium
ABAP CDs table function introduction and examples
Count the number of given strings in a string