当前位置:网站首页>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 !
边栏推荐
- Knowledge of two-dimensional array
- MySQL JPA support for JSON type data in database
- LeetCode 2351. 第一个出现两次的字母
- The cooperation between starfish OS and metabell is just the beginning
- LeetCode 2341. 数组能形成多少数对
- The understanding of domain adaptation in transfer learning and the introduction of three technologies
- Shaanxi Yuanjie semiconductor, a laser chip manufacturer, was invested by 8 investment institutions including GF Securities and CITIC Securities
- JUC concurrent programming learning
- Codeforces暑期训练周报(7.14~7.20)
- 如何让数字零售承接起流量时代和留量时代的发展重任,或许才是关键所在
猜你喜欢
随机推荐
URDF 集成 Gazebo
BYD semiconductor completed the a+ round financing of 800million yuan: 30 well-known investment institutions entered the market, with a valuation of 10.2 billion yuan!
Interpretation of new features | the restriction of MySQL 8.0 on gtid is lifted
Summary of common shortcut keys in idea
Lua快速上手
8000 word explanation of OBSA principle and application practice
2022/07/27 学习笔记 (day17) 代码块和内部类
JG data reset (WD)
Centralized management of clusters
20 bad habits of bad programmers
迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码
How to solve the pain points of 12000 small and medium-sized customers' component procurement? Say goodbye to overtime!
Introduction and configuration of vsftpd
Unknown database ‘xxxxx‘
比亚迪半导体完成8亿元A+轮融资:30家知名投资机构入局,估值已达102亿元!
Fabric2.4.4 version building process (complete process)
S-RPN: Sampling-balanced region proposal network for small crop pest detection
Icml2022 | online decision transformer
Kibana6.2.4 version update x-pack certification
Lua get started quickly



![[C language] file operation](/img/6e/b8f3466ca0a5f7424afcab561124af.png)




