当前位置:网站首页>How many pairs can an array of leetcode simple questions form
How many pairs can an array of leetcode simple questions form
2022-07-26 19:06:00 【·Starry Sea】
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)
Their thinking
To form a number pair, you must first find the character frequency of the array , Where the frequency is 2 Multiples of will not produce the remaining unpaired characters , If not 2 Multiples of can be generated 1 position single.
class Solution:
def numberOfPairs(self, nums: List[int]) -> List[int]:
couple,remain=0,0
for i in Counter(nums).values():
a,b=divmod(i,2)
couple+=a
remain+=b
return [couple,remain]

边栏推荐
- 图解用户登录验证流程,写得太好了!
- Have you ever encountered a deadlock problem in MySQL? How did you solve it?
- 2022年制冷与空调设备运行操作考试模拟100题及模拟考试
- Sentinel 隔离与降级
- Arrangement of information security emergency plan
- 当前占位,之后再写
- 【考研词汇训练营】Day 14 —— panini,predict,access,apologize,sense,transport,aggregation
- SD NAND与eMMC优劣势对比
- Multi merchant mall system function disassembly Lecture 16 - platform side member growth value record
- Ministry of Finance: IC design enterprises and software enterprises will be exempted from corporate income tax this year and next!
猜你喜欢

The pit of mpc5744p reports an error, RTOS cannot be started, and there is a clock source problem

Write a thesis and read this one

FTP协议

2022 welder (elementary) operation certificate examination question bank and simulation examination

ALV screen input option learning
![[soft exam] soft exam tutorial + real questions over the years](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[soft exam] soft exam tutorial + real questions over the years

2022 mobile crane driver test questions simulation test platform operation

MySQL learning notes -2. how to improve the query performance of SQL statements

MySQL - 多表查询与案例详解

MySQL - multi table query and case explanation
随机推荐
How far can Volvo, which has lost its "safety brand" due to declining sales and marginalization of its brand?
flex布局
JS question brushing plan - linked list
2022G1工业锅炉司炉上岗证题库及模拟考试
LeetCode简单题之装满杯子需要的最短总时长
[yuntu said] issue 246 digital asset chain - your God of digital asset property protection!
ALV screen input option learning
VTK (the Visualization Toolkit) loads STL models
Automated test tool playwright (quick start)
工赋开发者社区 | 定了!就在7月30日!
Lombok common notes
JS刷题计划——数组
简述MES系统的11大核心功能模块
2022上海市安全员C证操作证考试题库模拟考试平台操作
这场竞赛,能读懂题目的你大有可为
PMP candidates must read, and the epidemic prevention requirements for the exam on July 30 are here
Sudden! Arm stops cooperating with Huawei! How big is the impact on Huawei?
.Net CLR GC 动态加载短暂堆阈值的计算及阈值超量的计算
场景之分页查询设计
rancher部署kubernetes集群