Skip to content

Commit b97ce2d

Browse files
committed
feat: add solutions to lc problem: No.1512
No.1512.Number of Good Pairs
1 parent 6fdd953 commit b97ce2d

File tree

9 files changed

+284
-99
lines changed

9 files changed

+284
-99
lines changed

solution/1500-1599/1512.Number of Good Pairs/README.md

Lines changed: 130 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:计数**
50+
51+
遍历数组,对于每个元素 $x$,计算 $x$ 之前有多少个元素与其相等,即为 $x$ 与之前元素组成的好数对的数目。遍历完数组后,即可得到答案。
52+
53+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为数组长度,而 $C$ 为数组中元素的取值范围。本题中 $C = 101$。
54+
4955
<!-- tabs:start -->
5056

5157
### **Python3**
@@ -55,8 +61,19 @@
5561
```python
5662
class Solution:
5763
def numIdenticalPairs(self, nums: List[int]) -> int:
58-
counter = Counter(nums)
59-
return sum([x * (x - 1) for x in counter.values()]) >> 1
64+
ans = 0
65+
cnt = Counter()
66+
for x in nums:
67+
ans += cnt[x]
68+
cnt[x] += 1
69+
return ans
70+
```
71+
72+
```python
73+
class Solution:
74+
def numIdenticalPairs(self, nums: List[int]) -> int:
75+
cnt = Counter(nums)
76+
return sum(v * (v - 1) for v in cnt.values()) >> 1
6077
```
6178

6279
### **Java**
@@ -66,15 +83,28 @@ class Solution:
6683
```java
6784
class Solution {
6885
public int numIdenticalPairs(int[] nums) {
69-
Map<Integer, Integer> counter = new HashMap<>();
70-
for (int num : nums) {
71-
counter.put(num, counter.getOrDefault(num, 0) + 1);
86+
int ans = 0;
87+
int[] cnt = new int[101];
88+
for (int x : nums) {
89+
ans += cnt[x]++;
90+
}
91+
return ans;
92+
}
93+
}
94+
```
95+
96+
```java
97+
class Solution {
98+
public int numIdenticalPairs(int[] nums) {
99+
int[] cnt = new int[101];
100+
for (int x : nums) {
101+
++cnt[x];
72102
}
73-
int res = 0;
74-
for (int n : counter.values()) {
75-
res += n * (n - 1);
103+
int ans = 0;
104+
for (int v : cnt) {
105+
ans += v * (v - 1) / 2;
76106
}
77-
return res >> 1;
107+
return ans;
78108
}
79109
}
80110
```
@@ -85,58 +115,112 @@ class Solution {
85115
class Solution {
86116
public:
87117
int numIdenticalPairs(vector<int>& nums) {
88-
unordered_map<int, int> counter;
89-
for (int num : nums) {
90-
++counter[num];
118+
int ans = 0;
119+
int cnt[101]{};
120+
for (int& x : nums) {
121+
ans += cnt[x]++;
91122
}
92-
int res = 0;
93-
for (auto& [num, n] : counter) {
94-
res += n * (n - 1);
123+
return ans;
124+
}
125+
};
126+
```
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
int numIdenticalPairs(vector<int>& nums) {
132+
int cnt[101]{};
133+
for (int& x : nums) {
134+
++cnt[x];
135+
}
136+
int ans = 0;
137+
for (int v : cnt) {
138+
ans += v * (v - 1) / 2;
95139
}
96-
return res >> 1;
140+
return ans;
97141
}
98142
};
99143
```
100144

101145
### **Go**
102146

103147
```go
104-
func numIdenticalPairs(nums []int) int {
105-
counter := make(map[int]int)
106-
for _, num := range nums {
107-
counter[num]++
148+
func numIdenticalPairs(nums []int) (ans int) {
149+
cnt := [101]int{}
150+
for _, x := range nums {
151+
ans += cnt[x]
152+
cnt[x]++
153+
}
154+
return
155+
}
156+
```
157+
158+
```go
159+
func numIdenticalPairs(nums []int) (ans int) {
160+
cnt := [101]int{}
161+
for _, x := range nums {
162+
cnt[x]++
108163
}
109-
res := 0
110-
for _, n := range counter {
111-
res += n * (n - 1)
164+
for _, v := range cnt {
165+
ans += v * (v - 1) / 2
112166
}
113-
return res >> 1
167+
return
114168
}
115169
```
116170

117171
### **TypeScript**
118172

119173
```ts
120174
function numIdenticalPairs(nums: number[]): number {
121-
const count = new Array(101).fill(0);
175+
const cnt = new Array(101).fill(0);
122176
let ans = 0;
123-
for (const num of nums) {
124-
ans += count[num]++;
177+
for (const x of nums) {
178+
ans += cnt[x]++;
125179
}
126180
return ans;
127181
}
128182
```
129183

184+
```ts
185+
function numIdenticalPairs(nums: number[]): number {
186+
const cnt = new Array(101).fill(0);
187+
for (const x of nums) {
188+
++cnt[x];
189+
}
190+
let ans = 0;
191+
for (const v of cnt) {
192+
ans += v * (v - 1);
193+
}
194+
return ans >> 1;
195+
}
196+
```
197+
130198
### **Rust**
131199

132200
```rust
133201
impl Solution {
134202
pub fn num_identical_pairs(nums: Vec<i32>) -> i32 {
135-
let mut count = [0; 101];
203+
let mut cnt = [0; 101];
136204
let mut ans = 0;
137205
for &num in nums.iter() {
138-
ans += count[num as usize];
139-
count[num as usize] += 1;
206+
ans += cnt[num as usize];
207+
cnt[num as usize] += 1;
208+
}
209+
ans
210+
}
211+
}
212+
```
213+
214+
```rust
215+
impl Solution {
216+
pub fn num_identical_pairs(nums: Vec<i32>) -> i32 {
217+
let mut cnt = [0; 101];
218+
for &num in nums.iter() {
219+
cnt[num as usize] += 1;
220+
}
221+
let mut ans = 0;
222+
for &v in cnt.iter() {
223+
ans += v * (v - 1) / 2
140224
}
141225
ans
142226
}
@@ -147,10 +231,24 @@ impl Solution {
147231

148232
```c
149233
int numIdenticalPairs(int *nums, int numsSize) {
150-
int count[101] = {0};
234+
int cnt[101] = {0};
151235
int ans = 0;
152236
for (int i = 0; i < numsSize; i++) {
153-
ans += count[nums[i]]++;
237+
ans += cnt[nums[i]]++;
238+
}
239+
return ans;
240+
}
241+
```
242+
243+
```c
244+
int numIdenticalPairs(int *nums, int numsSize) {
245+
int cnt[101] = {0};
246+
for (int i = 0; i < numsSize; i++) {
247+
cnt[nums[i]]++;
248+
}
249+
int ans = 0;
250+
for (int i = 0; i < 101; ++i) {
251+
ans += cnt[i] * (cnt[i] - 1) / 2;
154252
}
155253
return ans;
156254
}

0 commit comments

Comments
 (0)