Skip to content

Commit 72adec5

Browse files
committed
feat: add solutions to lc problem: No.1814
No.1814.Count Nice Pairs in an Array
1 parent 2927315 commit 72adec5

File tree

6 files changed

+279
-2
lines changed

6 files changed

+279
-2
lines changed

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

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,121 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:式子变换 + 哈希表**
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]$ 作为哈希表的值,统计每个键出现的次数,然后计算每个键对应的值的组合数,最后将所有组合数相加即可。
51+
52+
时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组 `nums` 的长度和数组 `nums` 中的最大值。空间复杂度 $O(n)$。
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**
5157

5258
<!-- 这里可写当前语言的特殊实现逻辑 -->
5359

5460
```python
55-
61+
class Solution:
62+
def countNicePairs(self, nums: List[int]) -> int:
63+
def rev(x):
64+
y = 0
65+
while x:
66+
y = y * 10 + x % 10
67+
x //= 10
68+
return y
69+
70+
cnt = Counter(x - rev(x) for x in nums)
71+
ans = 0
72+
mod = 10**9 + 7
73+
for v in cnt.values():
74+
ans = (ans + v * (v - 1) // 2) % mod
75+
return ans
5676
```
5777

5878
### **Java**
5979

6080
<!-- 这里可写当前语言的特殊实现逻辑 -->
6181

6282
```java
83+
class Solution {
84+
private static final int MOD = (int) 1e9 + 7;
85+
86+
public int countNicePairs(int[] nums) {
87+
Map<Integer, Integer> cnt = new HashMap<>();
88+
for (int x : nums) {
89+
int y = x - rev(x);
90+
cnt.put(y, cnt.getOrDefault(y, 0) + 1);
91+
}
92+
long ans = 0;
93+
for (int v : cnt.values()) {
94+
ans = (ans + (long) v * (v - 1) / 2) % MOD;
95+
}
96+
return (int) ans;
97+
}
98+
99+
private int rev(int x) {
100+
int y = 0;
101+
while (x > 0) {
102+
y = y * 10 + x % 10;
103+
x /= 10;
104+
}
105+
return y;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
const int mod = 1e9 + 7;
116+
117+
int countNicePairs(vector<int>& nums) {
118+
unordered_map<int, int> cnt;
119+
for (int& x : nums) {
120+
int y = x - rev(x);
121+
cnt[y]++;
122+
}
123+
long long ans = 0;
124+
for (auto& [_, v] : cnt) {
125+
ans = (ans + 1ll * v * (v - 1) / 2) % mod;
126+
}
127+
return ans;
128+
}
129+
130+
int rev(int x) {
131+
int y = 0;
132+
while (x) {
133+
y = y * 10 + x % 10;
134+
x /= 10;
135+
}
136+
return y;
137+
}
138+
};
139+
```
63140

141+
### **Go**
142+
143+
```go
144+
func countNicePairs(nums []int) (ans int) {
145+
const mod int = 1e9 + 7
146+
rev := func(x int) (y int) {
147+
for x > 0 {
148+
y = y*10 + x%10
149+
x /= 10
150+
}
151+
return
152+
}
153+
cnt := map[int]int{}
154+
for _, x := range nums {
155+
y := x - rev(x)
156+
cnt[y]++
157+
}
158+
for _, v := range cnt {
159+
ans = (ans + v*(v-1)/2) % mod
160+
}
161+
return
162+
}
64163
```
65164

66165
### **...**

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

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,106 @@
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def countNicePairs(self, nums: List[int]) -> int:
51+
def rev(x):
52+
y = 0
53+
while x:
54+
y = y * 10 + x % 10
55+
x //= 10
56+
return y
57+
58+
cnt = Counter(x - rev(x) for x in nums)
59+
ans = 0
60+
mod = 10**9 + 7
61+
for v in cnt.values():
62+
ans = (ans + v * (v - 1) // 2) % mod
63+
return ans
5064
```
5165

5266
### **Java**
5367

5468
```java
69+
class Solution {
70+
private static final int MOD = (int) 1e9 + 7;
71+
72+
public int countNicePairs(int[] nums) {
73+
Map<Integer, Integer> cnt = new HashMap<>();
74+
for (int x : nums) {
75+
int y = x - rev(x);
76+
cnt.put(y, cnt.getOrDefault(y, 0) + 1);
77+
}
78+
long ans = 0;
79+
for (int v : cnt.values()) {
80+
ans = (ans + (long) v * (v - 1) / 2) % MOD;
81+
}
82+
return (int) ans;
83+
}
84+
85+
private int rev(int x) {
86+
int y = 0;
87+
while (x > 0) {
88+
y = y * 10 + x % 10;
89+
x /= 10;
90+
}
91+
return y;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
const int mod = 1e9 + 7;
102+
103+
int countNicePairs(vector<int>& nums) {
104+
unordered_map<int, int> cnt;
105+
for (int& x : nums) {
106+
int y = x - rev(x);
107+
cnt[y]++;
108+
}
109+
long long ans = 0;
110+
for (auto& [_, v] : cnt) {
111+
ans = (ans + 1ll * v * (v - 1) / 2) % mod;
112+
}
113+
return ans;
114+
}
115+
116+
int rev(int x) {
117+
int y = 0;
118+
while (x) {
119+
y = y * 10 + x % 10;
120+
x /= 10;
121+
}
122+
return y;
123+
}
124+
};
125+
```
55126

127+
### **Go**
128+
129+
```go
130+
func countNicePairs(nums []int) (ans int) {
131+
const mod int = 1e9 + 7
132+
rev := func(x int) (y int) {
133+
for x > 0 {
134+
y = y*10 + x%10
135+
x /= 10
136+
}
137+
return
138+
}
139+
cnt := map[int]int{}
140+
for _, x := range nums {
141+
y := x - rev(x)
142+
cnt[y]++
143+
}
144+
for _, v := range cnt {
145+
ans = (ans + v*(v-1)/2) % mod
146+
}
147+
return
148+
}
56149
```
57150

58151
### **...**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
const int mod = 1e9 + 7;
4+
5+
int countNicePairs(vector<int>& nums) {
6+
unordered_map<int, int> cnt;
7+
for (int& x : nums) {
8+
int y = x - rev(x);
9+
cnt[y]++;
10+
}
11+
long long ans = 0;
12+
for (auto& [_, v] : cnt) {
13+
ans = (ans + 1ll * v * (v - 1) / 2) % mod;
14+
}
15+
return ans;
16+
}
17+
18+
int rev(int x) {
19+
int y = 0;
20+
while (x) {
21+
y = y * 10 + x % 10;
22+
x /= 10;
23+
}
24+
return y;
25+
}
26+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func countNicePairs(nums []int) (ans int) {
2+
const mod int = 1e9 + 7
3+
rev := func(x int) (y int) {
4+
for x > 0 {
5+
y = y*10 + x%10
6+
x /= 10
7+
}
8+
return
9+
}
10+
cnt := map[int]int{}
11+
for _, x := range nums {
12+
y := x - rev(x)
13+
cnt[y]++
14+
}
15+
for _, v := range cnt {
16+
ans = (ans + v*(v-1)/2) % mod
17+
}
18+
return
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
public int countNicePairs(int[] nums) {
5+
Map<Integer, Integer> cnt = new HashMap<>();
6+
for (int x : nums) {
7+
int y = x - rev(x);
8+
cnt.put(y, cnt.getOrDefault(y, 0) + 1);
9+
}
10+
long ans = 0;
11+
for (int v : cnt.values()) {
12+
ans = (ans + (long) v * (v - 1) / 2) % MOD;
13+
}
14+
return (int) ans;
15+
}
16+
17+
private int rev(int x) {
18+
int y = 0;
19+
while (x > 0) {
20+
y = y * 10 + x % 10;
21+
x /= 10;
22+
}
23+
return y;
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def countNicePairs(self, nums: List[int]) -> int:
3+
def rev(x):
4+
y = 0
5+
while x:
6+
y = y * 10 + x % 10
7+
x //= 10
8+
return y
9+
10+
cnt = Counter(x - rev(x) for x in nums)
11+
ans = 0
12+
mod = 10**9 + 7
13+
for v in cnt.values():
14+
ans = (ans + v * (v - 1) // 2) % mod
15+
return ans

0 commit comments

Comments
 (0)