当前位置:网站首页>LeetCode. 302 weekly games___ 01_ 6120. How many pairs can an array form__ Simple hash
LeetCode. 302 weekly games___ 01_ 6120. How many pairs can an array form__ Simple hash
2022-07-21 01:42:00 【To the light】
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
Solution:
- We use hash directly to count numbers , Then traverse the hash table , For each element hash[i], stay hash[i] Not for 0 when ,hash[i] / 2 That is to say i
Number of pairs that can be formed , Then judge hash[i] %= 2 Of hash[i] Is there any remaining , If there is surplus, it will be calculated to answer[1] Then you can ;
Code:
class Solution {
public int[] numberOfPairs(int[] nums) {
int[] hash = new int[101];
int len = nums.length;
for(int i=0;i<len;i++){
hash[nums[i]]++;
}
int[] res = new int[2];
for(int i=0;i<101;i++){
if(hash[i] != 0){
res[0]+= (hash[i] / 2);
hash[i] %= 2;
if(hash[i] != 0){
res[1]++;
}
}
}
return res;
}
}
边栏推荐
猜你喜欢
PHP(1)
AT32使用内核DWT寄存器设定延时时间
Webrtc series -sdp and other relationships
Network security comprehensive penetration test cve-2010-2883-pdf vulnerability analysis
接口测试到底怎么做,看完这篇文章彻底搞清楚
Resolved no module named 'flask_ Misaka '[bug solution]
Record of force deduction and question brushing 2---35 Search insertion location
ShardingSphere-proxy 搭配 MogDB/openGauss 实现分布式数据库
Android开发Tencent的面试准备
10 个用于网络管理员进行高级扫描的端口扫描工具
随机推荐
中职网络安全技能大赛P100-Dcore(轻型CMS系统)SQL注入
接口测试到底怎么做,看完这篇文章彻底搞清楚
力扣刷题238.除自身以外数组的乘积
计算任意根号n的值
中职网络安全技能大赛P100-Web渗透测试
Tutorial on principles and applications of database systems (029) -- data integrity of MySQL (II): defining primary keys
力扣刷题记录1-----704.二分查找
Using TinyMCE rich text editor in vscode
力扣刷题26. 删除有序数组中的重复项
【服务器数据恢复】某品牌ProLiant服务器raid瘫痪数据库文件损坏的数据恢复
infraversion和superaversion
Android开发Tencent的面试准备
Tutorial on principles and applications of database system (030) -- data integrity of MySQL (III): defining unique constraints
Map/Multimap 容器的系列操作
为什么说巨星传奇(周杰伦)不构成传销?分销和传销有什么区别?
CCleaner的使用
都2022年了,你不会还不知道什么是自动化测试吧....
Configure dual database
VMware安装
LeetCode.1217. 玩筹码____简单贪心