当前位置:网站首页>Leetcode-1512. Number of good pairs
Leetcode-1512. Number of good pairs
2022-06-12 05:59:00 【Taoist scholar】
link
subject
Give you an array of integers nums .
If a set of numbers (i,j) Satisfy nums[i] == nums[j] And i < j , You can think of this as a group Good number of pairs .
Returns the number of good pairs .
Example
Example 1:
Input :nums = [1,2,3,1,1,3]
Output :4
explain : Yes 4 Group several pairs , Namely (0,3), (0,4), (3,4), (2,5) , Subscript from 0 StartExample 2:
Input :nums = [1,1,1,1]
Output :6
explain : Each group of numbers in the array is a good number pairExample 3:
Input :nums = [1,2,3]
Output :0
explain
1 <= nums.length <= 1001 <= nums[i] <= 100
Ideas
Let's give you an example ,[1,1,1], For this array , When traversing to the subscript 1 Of 1 When , Can be subscripted with 0 Of 1 Form a pair of answers , When traversing to the subscript 2 Of 1 When , There are two in front 1, Therefore, it can form 2 The answer .
So we can traverse the array while , Use a hash table to record the current number of each element , How many elements of this element have appeared before , How many pairs can be added to the answer .
C++ Code
class Solution {
public:
int numIdenticalPairs(vector<int>& nums) {
unordered_map <int, int> map;
int ans = 0;
for (int num: nums)
{
ans+=map[num];
map[num]++;
}
return ans;
}
};边栏推荐
- Memory model, reference and function supplement of program
- The application could not be installed: INSTALL_FAILED_TEST_ONLY
- Identification of campus green plants based on tensorflow
- 基于tensorflow的校园绿植识别
- Leetcode 第 80 场双周赛题解
- Liunx Foundation
- Jackson - how to convert the array string with only one map object to list < map >
- Error the main class com xxx. yyy. Application
- flex/fixed上中下(移動端)
- Introduction to redis high availability
猜你喜欢

Go interface oriented programming practice

为什么联合索引是最左匹配原则?

Available RTMP and RTSP test addresses of the public network (updated in March, 2021)

March 23, 2021

Lock and reentrankload

Introduction to thread pool: ThreadPoolExecutor

Win10 desktop unlimited refresh

肝了一个月的 DDD,一文带你掌握

EBook upload

Introduction to sringmvc
随机推荐
Redis transaction
Win10 desktop unlimited refresh
Research Report on water sports shoes industry - market status analysis and development prospect forecast
March 4, 2021
Word frequency statistics using Jieba database
Leetcode-2048. Next larger numerical balance
Flutter monitors application lifecycle status
Types, functions and applications of intelligent sensors
[road of system analyst] collection of wrong topics in software engineering chapters
Guns框架多数据源配置,不修改配置文件
nus_data_handler源码解读结构体等数据类型
Understanding of distributed transactions
What is the lszrz protocol used at ordinary times? Talk about xmodem/ymodem/zmodem
Leetcode-1604. Warning people who use the same employee card more than or equal to three times within one hour
Rtmp/rtsp/hls public network real available test address
March 22, 2021
Leetcode-1552. Magnetic force between two balls
User login (medium)
Json-c common APIs
Leetcode-1260. 2D mesh migration