diff --git a/solution/1800-1899/1814.Count Nice Pairs in an Array/README.md b/solution/1800-1899/1814.Count Nice Pairs in an Array/README.md index 3fe311242d8fd..cbca81f3085cf 100644 --- a/solution/1800-1899/1814.Count Nice Pairs in an Array/README.md +++ b/solution/1800-1899/1814.Count Nice Pairs in an Array/README.md @@ -322,6 +322,35 @@ function countNicePairs(nums: number[]): number { } ``` +### **C#** + +```cs +public class Solution { + public int CountNicePairs(int[] nums) { + Dictionary cnt = new Dictionary(); + foreach (int x in nums) { + int y = x - Rev(x); + cnt[y] = cnt.GetValueOrDefault(y, 0) + 1; + } + int mod = (int)1e9 + 7; + long ans = 0; + foreach (int v in cnt.Values) { + ans = (ans + (long)v * (v - 1) / 2) % mod; + } + return (int)ans; + } + + private int Rev(int x) { + int y = 0; + while (x > 0) { + y = y * 10 + x % 10; + x /= 10; + } + return y; + } +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md b/solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md index 3f4a53da9142d..c2033412e16f1 100644 --- a/solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md +++ b/solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md @@ -314,6 +314,35 @@ function countNicePairs(nums: number[]): number { } ``` +### **C#** + +```cs +public class Solution { + public int CountNicePairs(int[] nums) { + Dictionary cnt = new Dictionary(); + foreach (int x in nums) { + int y = x - Rev(x); + cnt[y] = cnt.GetValueOrDefault(y, 0) + 1; + } + int mod = (int)1e9 + 7; + long ans = 0; + foreach (int v in cnt.Values) { + ans = (ans + (long)v * (v - 1) / 2) % mod; + } + return (int)ans; + } + + private int Rev(int x) { + int y = 0; + while (x > 0) { + y = y * 10 + x % 10; + x /= 10; + } + return y; + } +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1814.Count Nice Pairs in an Array/Solution.cs b/solution/1800-1899/1814.Count Nice Pairs in an Array/Solution.cs new file mode 100644 index 0000000000000..45a7d349c100f --- /dev/null +++ b/solution/1800-1899/1814.Count Nice Pairs in an Array/Solution.cs @@ -0,0 +1,24 @@ +public class Solution { + public int CountNicePairs(int[] nums) { + Dictionary cnt = new Dictionary(); + foreach (int x in nums) { + int y = x - Rev(x); + cnt[y] = cnt.GetValueOrDefault(y, 0) + 1; + } + int mod = (int)1e9 + 7; + long ans = 0; + foreach (int v in cnt.Values) { + ans = (ans + (long)v * (v - 1) / 2) % mod; + } + return (int)ans; + } + + private int Rev(int x) { + int y = 0; + while (x > 0) { + y = y * 10 + x % 10; + x /= 10; + } + return y; + } +}