46
46
47
47
<!-- 这里可写通用的实现逻辑 -->
48
48
49
+ ** 方法一:计数**
50
+
51
+ 遍历数组,对于每个元素 $x$,计算 $x$ 之前有多少个元素与其相等,即为 $x$ 与之前元素组成的好数对的数目。遍历完数组后,即可得到答案。
52
+
53
+ 时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为数组长度,而 $C$ 为数组中元素的取值范围。本题中 $C = 101$。
54
+
49
55
<!-- tabs:start -->
50
56
51
57
### ** Python3**
55
61
``` python
56
62
class Solution :
57
63
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
60
77
```
61
78
62
79
### ** Java**
@@ -66,15 +83,28 @@ class Solution:
66
83
``` java
67
84
class Solution {
68
85
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];
72
102
}
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 ;
76
106
}
77
- return res >> 1 ;
107
+ return ans ;
78
108
}
79
109
}
80
110
```
@@ -85,58 +115,112 @@ class Solution {
85
115
class Solution {
86
116
public:
87
117
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] ++;
91
122
}
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;
95
139
}
96
- return res >> 1 ;
140
+ return ans ;
97
141
}
98
142
};
99
143
```
100
144
101
145
### ** Go**
102
146
103
147
``` 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]++
108
163
}
109
- res := 0
110
- for _, n := range counter {
111
- res += n * (n - 1)
164
+ for _ , v := range cnt {
165
+ ans += v * (v - 1 ) / 2
112
166
}
113
- return res >> 1
167
+ return
114
168
}
115
169
```
116
170
117
171
### ** TypeScript**
118
172
119
173
``` ts
120
174
function numIdenticalPairs(nums : number []): number {
121
- const count = new Array (101 ).fill (0 );
175
+ const cnt = new Array (101 ).fill (0 );
122
176
let ans = 0 ;
123
- for (const num of nums ) {
124
- ans += count [ num ]++ ;
177
+ for (const x of nums ) {
178
+ ans += cnt [ x ]++ ;
125
179
}
126
180
return ans ;
127
181
}
128
182
```
129
183
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
+
130
198
### ** Rust**
131
199
132
200
``` rust
133
201
impl Solution {
134
202
pub fn num_identical_pairs (nums : Vec <i32 >) -> i32 {
135
- let mut count = [0 ; 101 ];
203
+ let mut cnt = [0 ; 101 ];
136
204
let mut ans = 0 ;
137
205
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
140
224
}
141
225
ans
142
226
}
@@ -147,10 +231,24 @@ impl Solution {
147
231
148
232
``` c
149
233
int numIdenticalPairs (int * nums, int numsSize) {
150
- int count [ 101] = {0};
234
+ int cnt [ 101] = {0};
151
235
int ans = 0;
152
236
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;
154
252
}
155
253
return ans;
156
254
}
0 commit comments