Skip to content

Commit 2fb53ed

Browse files
authored
feat: add cs solution to lc problem: No.1814 (#1997)
1 parent ba842d7 commit 2fb53ed

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

solution/1800-1899/1814.Count Nice Pairs in an Array/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,35 @@ function countNicePairs(nums: number[]): number {
322322
}
323323
```
324324

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)1e9 + 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+
325354
### **...**
326355

327356
```

solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,35 @@ function countNicePairs(nums: number[]): number {
314314
}
315315
```
316316

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)1e9 + 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+
317346
### **...**
318347

319348
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)