当前位置:网站首页>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 !
边栏推荐
- Xinyi information technology, a domestic NB IOT chip manufacturer, received 200million yuan of a+ round financing
- 软件测试面试题:如何准备测试数据?如何防止数据污染?
- 彻底搞懂kubernetes调度框架与插件
- Day 013 一维数组练习
- EWM receiving ECC delivery note verification logic problem
- 软件测试面试题:think_time的作用是什么?
- Centralized management of clusters
- Cesium add light sweep
- LeetCode 2341. 数组能形成多少数对
- 华为旗下哈勃投资入股VCSEL芯片厂商纵慧芯光
猜你喜欢

JUC concurrent programming learning

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

Principle of logistic regression

二维数组相关知识

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

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

【C语言】文件操作

Learn how Baidu PaddlePaddle easydl realizes automatic animal recognition in aquarium

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

Data problems can also be found if there is a space at the end of the field value of MySQL query criteria
随机推荐
3年经验想拿20K,居然面了半个月都没拿到?
Codeforces暑期训练周报(7.21~7.27)
深圳华强宣布拟不超2000万元入股比亚迪半导体
From functional testing to automated testing, my monthly salary has exceeded 30k+, and I have 6 years of testing experience.
Lecture 16 of project practice: using the open close principle to realize the commodity price rule engine
S-RPN: Sampling-balanced region proposal network for small crop pest detection
Fluent call interface UI
How to make digital retail undertake the development task of the era of traffic and retention may be the key
Three basic teaching
Briefly understand namenode and datanode
Gazebo control example
Codeforces summer training weekly (7.14~7.20)
登录功能实现
国产NB-IoT芯片厂商芯翼信息科技获2亿元A+轮融资
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
对迁移学习中域适应的理解和3种技术的介绍
2022/07/27 学习笔记 (day17) 代码块和内部类
糟糕程序员的20个坏习惯
Cross domain requests in nodejs
JG data reset (WD)