File tree 3 files changed +82
-0
lines changed
solution/1800-1899/1814.Count Nice Pairs in an Array
3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change @@ -322,6 +322,35 @@ function countNicePairs(nums: number[]): number {
322
322
}
323
323
```
324
324
325
+ ### ** C#**
326
+
327
+ ``` cs
328
+ public class Solution {
329
+ public int CountNicePairs (int [] nums ) {
330
+ Dictionary < int , int > cnt = new Dictionary <int , int >();
331
+ foreach (int x in nums ) {
332
+ int y = x - Rev (x );
333
+ cnt [y ] = cnt .GetValueOrDefault (y , 0 ) + 1 ;
334
+ }
335
+ int mod = (int )1 e 9 + 7 ;
336
+ long ans = 0 ;
337
+ foreach (int v in cnt .Values ) {
338
+ ans = (ans + (long )v * (v - 1 ) / 2 ) % mod ;
339
+ }
340
+ return (int )ans ;
341
+ }
342
+
343
+ private int Rev (int x ) {
344
+ int y = 0 ;
345
+ while (x > 0 ) {
346
+ y = y * 10 + x % 10 ;
347
+ x /= 10 ;
348
+ }
349
+ return y ;
350
+ }
351
+ }
352
+ ```
353
+
325
354
### ** ...**
326
355
327
356
```
Original file line number Diff line number Diff line change @@ -314,6 +314,35 @@ function countNicePairs(nums: number[]): number {
314
314
}
315
315
```
316
316
317
+ ### ** C#**
318
+
319
+ ``` cs
320
+ public class Solution {
321
+ public int CountNicePairs (int [] nums ) {
322
+ Dictionary < int , int > cnt = new Dictionary <int , int >();
323
+ foreach (int x in nums ) {
324
+ int y = x - Rev (x );
325
+ cnt [y ] = cnt .GetValueOrDefault (y , 0 ) + 1 ;
326
+ }
327
+ int mod = (int )1 e 9 + 7 ;
328
+ long ans = 0 ;
329
+ foreach (int v in cnt .Values ) {
330
+ ans = (ans + (long )v * (v - 1 ) / 2 ) % mod ;
331
+ }
332
+ return (int )ans ;
333
+ }
334
+
335
+ private int Rev (int x ) {
336
+ int y = 0 ;
337
+ while (x > 0 ) {
338
+ y = y * 10 + x % 10 ;
339
+ x /= 10 ;
340
+ }
341
+ return y ;
342
+ }
343
+ }
344
+ ```
345
+
317
346
### ** ...**
318
347
319
348
```
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int CountNicePairs ( int [ ] nums ) {
3
+ Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ;
4
+ foreach ( int x in nums ) {
5
+ int y = x - Rev ( x ) ;
6
+ cnt [ y ] = cnt . GetValueOrDefault ( y , 0 ) + 1 ;
7
+ }
8
+ int mod = ( int ) 1e9 + 7 ;
9
+ long ans = 0 ;
10
+ foreach ( int v in cnt . Values ) {
11
+ ans = ( ans + ( long ) v * ( v - 1 ) / 2 ) % mod ;
12
+ }
13
+ return ( int ) ans ;
14
+ }
15
+
16
+ private int Rev ( int x ) {
17
+ int y = 0 ;
18
+ while ( x > 0 ) {
19
+ y = y * 10 + x % 10 ;
20
+ x /= 10 ;
21
+ }
22
+ return y ;
23
+ }
24
+ }
You can’t perform that action at this time.
0 commit comments