Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78e99bf

Browse files
committed
feat: add solutions to lc problem: No.1814
No.1814.Count Nice Pairs in an Array
1 parent b5d5ff8 commit 78e99bf

File tree

11 files changed

+365
-80
lines changed

11 files changed

+365
-80
lines changed

‎solution/1800-1899/1813.Sentence Similarity III/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ class Solution:
9494
```java
9595
class Solution {
9696
public boolean areSentencesSimilar(String sentence1, String sentence2) {
97-
String[] words1 = sentence1.split(" ");
98-
String[] words2 = sentence2.split(" ");
97+
var words1 = sentence1.split(" ");
98+
var words2 = sentence2.split(" ");
9999
if (words1.length < words2.length) {
100-
String[] t = words1;
100+
var t = words1;
101101
words1 = words2;
102102
words2 = t;
103103
}
@@ -120,8 +120,8 @@ class Solution {
120120
class Solution {
121121
public:
122122
bool areSentencesSimilar(string sentence1, string sentence2) {
123-
vector<string> words1 = split(sentence1, ' ');
124-
vector<string> words2 = split(sentence2, ' ');
123+
auto words1 = split(sentence1, ' ');
124+
auto words2 = split(sentence2, ' ');
125125
if (words1.size() < words2.size()) {
126126
swap(words1, words2);
127127
}

‎solution/1800-1899/1813.Sentence Similarity III/README_EN.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ class Solution:
7171
```java
7272
class Solution {
7373
public boolean areSentencesSimilar(String sentence1, String sentence2) {
74-
String[] words1 = sentence1.split(" ");
75-
String[] words2 = sentence2.split(" ");
74+
var words1 = sentence1.split(" ");
75+
var words2 = sentence2.split(" ");
7676
if (words1.length < words2.length) {
77-
String[] t = words1;
77+
var t = words1;
7878
words1 = words2;
7979
words2 = t;
8080
}
@@ -97,8 +97,8 @@ class Solution {
9797
class Solution {
9898
public:
9999
bool areSentencesSimilar(string sentence1, string sentence2) {
100-
vector<string> words1 = split(sentence1, ' ');
101-
vector<string> words2 = split(sentence2, ' ');
100+
auto words1 = split(sentence1, ' ');
101+
auto words2 = split(sentence2, ' ');
102102
if (words1.size() < words2.size()) {
103103
swap(words1, words2);
104104
}

‎solution/1800-1899/1813.Sentence Similarity III/Solution.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public:
33
bool areSentencesSimilar(string sentence1, string sentence2) {
4-
vector<string> words1 = split(sentence1, ' ');
5-
vector<string> words2 = split(sentence2, ' ');
4+
auto words1 = split(sentence1, ' ');
5+
auto words2 = split(sentence2, ' ');
66
if (words1.size() < words2.size()) {
77
swap(words1, words2);
88
}

‎solution/1800-1899/1813.Sentence Similarity III/Solution.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution {
22
public boolean areSentencesSimilar(String sentence1, String sentence2) {
3-
String[] words1 = sentence1.split(" ");
4-
String[] words2 = sentence2.split(" ");
3+
var words1 = sentence1.split(" ");
4+
var words2 = sentence2.split(" ");
55
if (words1.length < words2.length) {
6-
String[] t = words1;
6+
var t = words1;
77
words1 = words2;
88
words2 = t;
99
}

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

Lines changed: 158 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747

4848
**方法一:式子变换 + 哈希表**
4949

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+
注意答案的取模操作。
5155

5256
时间复杂度 $O(n \times \log M),ドル其中 $n$ 和 $M$ 分别是数组 `nums` 的长度和数组 `nums` 中的最大值。空间复杂度 $O(n)$。
5357

@@ -68,11 +72,28 @@ class Solution:
6872
return y
6973

7074
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+
7189
ans = 0
7290
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
7697
```
7798

7899
### **Java**
@@ -81,26 +102,48 @@ class Solution:
81102

82103
```java
83104
class Solution {
84-
private static final int MOD = (int) 1e9 + 7;
85-
86105
public int countNicePairs(int[] nums) {
87106
Map<Integer, Integer> cnt = new HashMap<>();
88107
for (int x : nums) {
89108
int y = x - rev(x);
90-
cnt.put(y, cnt.getOrDefault(y, 0) +1);
109+
cnt.merge(y, 1, Integer::sum);
91110
}
111+
final int mod = (int) 1e9 + 7;
92112
long ans = 0;
93113
for (int v : cnt.values()) {
94-
ans = (ans + (long) v * (v - 1) / 2) % MOD;
114+
ans = (ans + (long) v * (v - 1) / 2) % mod;
95115
}
96116
return (int) ans;
97117
}
98118

99119
private int rev(int x) {
100120
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) {
102146
y = y * 10 + x % 10;
103-
x /= 10;
104147
}
105148
return y;
106149
}
@@ -112,28 +155,49 @@ class Solution {
112155
```cpp
113156
class Solution {
114157
public:
115-
const int mod = 1e9 + 7;
116-
117158
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+
};
118166
unordered_map<int, int> cnt;
119167
for (int& x : nums) {
120168
int y = x - rev(x);
121169
cnt[y]++;
122170
}
123171
long long ans = 0;
172+
const int mod = 1e9 + 7;
124173
for (auto& [_, v] : cnt) {
125174
ans = (ans + 1ll * v * (v - 1) / 2) % mod;
126175
}
127176
return ans;
128177
}
178+
};
179+
```
129180
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]++;
135199
}
136-
return y;
200+
return ans;
137201
}
138202
};
139203
```
@@ -142,11 +206,9 @@ public:
142206

143207
```go
144208
func countNicePairs(nums []int) (ans int) {
145-
const mod int = 1e9 + 7
146209
rev := func(x int) (y int) {
147-
for x > 0 {
210+
for ; x > 0; x /= 10 {
148211
y = y*10 + x%10
149-
x /= 10
150212
}
151213
return
152214
}
@@ -155,13 +217,88 @@ func countNicePairs(nums []int) (ans int) {
155217
y := x - rev(x)
156218
cnt[y]++
157219
}
220+
const mod int = 1e9 + 7
158221
for _, v := range cnt {
159222
ans = (ans + v*(v-1)/2) % mod
160223
}
161224
return
162225
}
163226
```
164227

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+
165302
### **...**
166303

167304
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /