当前位置:网站首页>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 !
边栏推荐
- spreadsheet 导出 excel表格
- idea常用的快捷键汇总
- Xinyi information technology, a domestic NB IOT chip manufacturer, received 200million yuan of a+ round financing
- 糟糕程序员的20个坏习惯
- The cooperation between starfish OS and metabell is just the beginning
- Icml2022 | online decision transformer
- Monitor mouse sideslip (adapt to mobile terminal)
- Token is used in nodejs
- BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)
- Tool function: pay the non empty field value in one workspace to the same field in another workspace
猜你喜欢

BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)

EWM receiving ECC delivery note verification logic problem

Can anime characters become "real people"? Paddegan helps you find the TA of "tear man"

Realize ABCD letter increment

BAT大厂测试架构师如何解读测试平台的各种争议

JUC concurrent programming learning

Codeforces暑期训练周报(7.14~7.20)

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

The cooperation between starfish OS and metabell is just the beginning

LeetCode 2351. 第一个出现两次的字母
随机推荐
数仓搭建——DWS层
Standing at the crossroads of digital retail transformation, we need to look at it from a new perspective
spreadsheet 导出 excel表格
开发 Flutter 录音功能的插件
C language main function transfer parameters
Realize ABCD letter increment
3000 words and 11 pictures hard core popular science: what is edge computing? What are the connections and differences with cloud computing?
彻底搞懂kubernetes调度框架与插件
Kibana6.2.4 version update x-pack certification
URDF integrated gazebo
Qlib教程——基于源码(二)本地数据保存与加载
[game] Nintendo Nintendo switch ultra detailed purchase / use guide and precautions (continuous update according to your own use...)
杂谈:一份最初就非常完善的FS跟第一版程序就要求没bug一样不切实际
面试题 01.07. 旋转矩阵
登录功能实现
Adding custom dynamic arts and Sciences to cesium
LeetCode 2347. 最好的扑克手牌
Huami technology "Huangshan No.2" release: AI performance is improved by 7 times, and power consumption is reduced by 50%!
Develop plug-ins for the recording function of flutter
Software process that testers need to know