47
47
48
48
** 方法一:式子变换 + 哈希表**
49
49
50
- 对于下标对 $(i, j)$,如果满足条件,那么有 $nums[ i] + rev(nums[ j] ) = nums[ j] + rev(nums[ i] )$,即 $nums[ i] - rev(nums[ i] ) = nums[ j] - rev(nums[ j] )$。移项后,有 $nums[ i] - nums[ j] = rev(nums[ j] ) - rev(nums[ i] )$。因此,我们可以将 $nums[ i] - rev(nums[ i] )$ 作为哈希表的键,将 $nums[ i] - nums[ j] $ 作为哈希表的值,统计每个键出现的次数,然后计算每个键对应的值的组合数,最后将所有组合数相加即可。
50
+ 对于下标对 $(i, j)$,如果满足条件,那么有 $nums[ i] + rev(nums[ j] ) = nums[ j] + rev(nums[ i] )$,即 $nums[ i] - rev(nums[ i] ) = nums[ j] - rev(nums[ j] )$。移项后,有 $nums[ i] - nums[ j] = rev(nums[ j] ) - rev(nums[ i] )$。
51
+
52
+ 因此,我们可以将 $nums[ i] - rev(nums[ i] )$ 作为哈希表的键,统计每个键出现的次数。最后计算每个键对应的值的组合数,相加得到最终的答案。
53
+
54
+ 注意答案的取模操作。
51
55
52
56
时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组 ` nums ` 的长度和数组 ` nums ` 中的最大值。空间复杂度 $O(n)$。
53
57
@@ -68,11 +72,28 @@ class Solution:
68
72
return y
69
73
70
74
cnt = Counter(x - rev(x) for x in nums)
75
+ mod = 10 ** 9 + 7
76
+ return sum (v * (v - 1 ) // 2 for v in cnt.values()) % mod
77
+ ```
78
+
79
+ ``` python
80
+ class Solution :
81
+ def countNicePairs (self , nums : List[int ]) -> int :
82
+ def rev (x ):
83
+ y = 0
84
+ while x:
85
+ y = y * 10 + x % 10
86
+ x //= 10
87
+ return y
88
+
71
89
ans = 0
72
90
mod = 10 ** 9 + 7
73
- for v in cnt.values():
74
- ans = (ans + v * (v - 1 ) // 2 ) % mod
75
- return ans
91
+ cnt = Counter()
92
+ for x in nums:
93
+ y = x - rev(x)
94
+ ans += cnt[y]
95
+ cnt[y] += 1
96
+ return ans % mod
76
97
```
77
98
78
99
### ** Java**
@@ -81,26 +102,48 @@ class Solution:
81
102
82
103
``` java
83
104
class Solution {
84
- private static final int MOD = (int ) 1e9 + 7 ;
85
-
86
105
public int countNicePairs (int [] nums ) {
87
106
Map<Integer , Integer > cnt = new HashMap<> ();
88
107
for (int x : nums) {
89
108
int y = x - rev(x);
90
- cnt. put (y, cnt . getOrDefault(y, 0 ) + 1 );
109
+ cnt. merge (y, 1 , Integer :: sum );
91
110
}
111
+ final int mod = (int ) 1e9 + 7 ;
92
112
long ans = 0 ;
93
113
for (int v : cnt. values()) {
94
- ans = (ans + (long ) v * (v - 1 ) / 2 ) % MOD ;
114
+ ans = (ans + (long ) v * (v - 1 ) / 2 ) % mod ;
95
115
}
96
116
return (int ) ans;
97
117
}
98
118
99
119
private int rev (int x ) {
100
120
int y = 0 ;
101
- while (x > 0 ) {
121
+ for (; x > 0 ; x /= 10 ) {
122
+ y = y * 10 + x % 10 ;
123
+ }
124
+ return y;
125
+ }
126
+ }
127
+ ```
128
+
129
+ ``` java
130
+ class Solution {
131
+ public int countNicePairs (int [] nums ) {
132
+ Map<Integer , Integer > cnt = new HashMap<> ();
133
+ final int mod = (int ) 1e9 + 7 ;
134
+ int ans = 0 ;
135
+ for (int x : nums) {
136
+ int y = x - rev(x);
137
+ ans = (ans + cnt. getOrDefault(y, 0 )) % mod;
138
+ cnt. merge(y, 1 , Integer :: sum);
139
+ }
140
+ return ans;
141
+ }
142
+
143
+ private int rev (int x ) {
144
+ int y = 0 ;
145
+ for (; x > 0 ; x /= 10 ) {
102
146
y = y * 10 + x % 10 ;
103
- x /= 10 ;
104
147
}
105
148
return y;
106
149
}
@@ -112,28 +155,49 @@ class Solution {
112
155
``` cpp
113
156
class Solution {
114
157
public:
115
- const int mod = 1e9 + 7;
116
-
117
158
int countNicePairs(vector<int >& nums) {
159
+ auto rev = [ ] (int x) {
160
+ int y = 0;
161
+ for (; x > 0; x /= 10) {
162
+ y = y * 10 + x % 10;
163
+ }
164
+ return y;
165
+ };
118
166
unordered_map<int, int> cnt;
119
167
for (int& x : nums) {
120
168
int y = x - rev(x);
121
169
cnt[ y] ++;
122
170
}
123
171
long long ans = 0;
172
+ const int mod = 1e9 + 7;
124
173
for (auto& [ _ , v] : cnt) {
125
174
ans = (ans + 1ll * v * (v - 1) / 2) % mod;
126
175
}
127
176
return ans;
128
177
}
178
+ };
179
+ ```
129
180
130
- int rev(int x) {
131
- int y = 0;
132
- while (x) {
133
- y = y * 10 + x % 10;
134
- x /= 10;
181
+ ```cpp
182
+ class Solution {
183
+ public:
184
+ int countNicePairs(vector<int>& nums) {
185
+ auto rev = [](int x) {
186
+ int y = 0;
187
+ for (; x > 0; x /= 10) {
188
+ y = y * 10 + x % 10;
189
+ }
190
+ return y;
191
+ };
192
+ unordered_map<int, int> cnt;
193
+ int ans = 0;
194
+ const int mod = 1e9 + 7;
195
+ for (int& x : nums) {
196
+ int y = x - rev(x);
197
+ ans = (ans + cnt[y]) % mod;
198
+ cnt[y]++;
135
199
}
136
- return y ;
200
+ return ans ;
137
201
}
138
202
};
139
203
```
@@ -142,11 +206,9 @@ public:
142
206
143
207
``` go
144
208
func countNicePairs (nums []int ) (ans int ) {
145
- const mod int = 1e9 + 7
146
209
rev := func (x int ) (y int ) {
147
- for x > 0 {
210
+ for ; x > 0 ; x /= 10 {
148
211
y = y*10 + x%10
149
- x /= 10
150
212
}
151
213
return
152
214
}
@@ -155,13 +217,88 @@ func countNicePairs(nums []int) (ans int) {
155
217
y := x - rev (x)
156
218
cnt[y]++
157
219
}
220
+ const mod int = 1e9 + 7
158
221
for _ , v := range cnt {
159
222
ans = (ans + v*(v-1 )/2 ) % mod
160
223
}
161
224
return
162
225
}
163
226
```
164
227
228
+ ``` go
229
+ func countNicePairs (nums []int ) (ans int ) {
230
+ rev := func (x int ) (y int ) {
231
+ for ; x > 0 ; x /= 10 {
232
+ y = y*10 + x%10
233
+ }
234
+ return
235
+ }
236
+ cnt := map [int ]int {}
237
+ const mod int = 1e9 + 7
238
+ for _ , x := range nums {
239
+ y := x - rev (x)
240
+ ans = (ans + cnt[y]) % mod
241
+ cnt[y]++
242
+ }
243
+ return
244
+ }
245
+ ```
246
+
247
+ ### ** JavaScript**
248
+
249
+ ``` js
250
+ /**
251
+ * @param {number[]} nums
252
+ * @return {number}
253
+ */
254
+ var countNicePairs = function (nums ) {
255
+ const rev = x => {
256
+ let y = 0 ;
257
+ for (; x > 0 ; x = Math .floor (x / 10 )) {
258
+ y = y * 10 + (x % 10 );
259
+ }
260
+ return y;
261
+ };
262
+ const cnt = new Map ();
263
+ for (const x of nums) {
264
+ const y = x - rev (x);
265
+ cnt .set (y, (cnt .get (y) | 0 ) + 1 );
266
+ }
267
+ let ans = 0 ;
268
+ const mod = 1e9 + 7 ;
269
+ for (const [_ , v ] of cnt) {
270
+ ans = (ans + Math .floor ((v * (v - 1 )) / 2 )) % mod;
271
+ }
272
+ return ans;
273
+ };
274
+ ```
275
+
276
+ ``` js
277
+ /**
278
+ * @param {number[]} nums
279
+ * @return {number}
280
+ */
281
+ var countNicePairs = function (nums ) {
282
+ const rev = x => {
283
+ let y = 0 ;
284
+ for (; x > 0 ; x = Math .floor (x / 10 )) {
285
+ y = y * 10 + (x % 10 );
286
+ }
287
+ return y;
288
+ };
289
+ let ans = 0 ;
290
+ const mod = 1e9 + 7 ;
291
+ const cnt = new Map ();
292
+ for (const x of nums) {
293
+ const y = x - rev (x);
294
+ const v = cnt .get (y) | 0 ;
295
+ ans = (ans + v) % mod;
296
+ cnt .set (y, v + 1 );
297
+ }
298
+ return ans;
299
+ };
300
+ ```
301
+
165
302
### ** ...**
166
303
167
304
```
0 commit comments