Skip to content

Commit 49b548d

Browse files
committed
update: 添加问题“2176.统计数组中相等且可以被整除的数对”的代码和题解(#885)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent db1ab87 commit 49b548d

8 files changed

+226
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-17 20:13:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-17 20:28:40
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countPairs(vector<int>& nums, int k) {
14+
int ans = 0;
15+
for (int i = 0; i < nums.size(); i++) {
16+
for (int j = i + 1; j < nums.size(); j++) {
17+
if (nums[i] == nums[j] && i * j % k == 0) {
18+
ans++;
19+
}
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-17 20:19:13
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-17 20:21:29
6+
*/
7+
package main
8+
9+
func countPairs(nums []int, k int) (ans int) {
10+
for i := range nums {
11+
for j := i + 1; j < len(nums); j++ {
12+
if nums[i] == nums[j] && i * j % k == 0 {
13+
ans++
14+
}
15+
}
16+
}
17+
return
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-17 20:17:48
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-17 20:18:12
6+
*/
7+
class Solution {
8+
public int countPairs(int[] nums, int k) {
9+
int ans = 0;
10+
for (int i = 0; i < nums.length; i++) {
11+
for (int j = i + 1; j < nums.length; j++) {
12+
if (nums[i] == nums[j] && i * j % k == 0) {
13+
ans++;
14+
}
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-17 20:15:46
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-17 20:15:58
6+
'''
7+
from typing import List
8+
9+
10+
class Solution:
11+
def countPairs(self, nums: List[int], k: int) -> int:
12+
return sum(nums[i] == nums[j] and i * j % k == 0 for i in range(len(nums)) for j in range(i + 1, len(nums)))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@
669669
|2132.用邮票贴满网格图|困难|<a href="https://leetcode.cn/problems/stamping-the-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/14/LeetCode%202132.%E7%94%A8%E9%82%AE%E7%A5%A8%E8%B4%B4%E6%BB%A1%E7%BD%91%E6%A0%BC%E5%9B%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135002925" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/stamping-the-grid/solutions/2566644/letmefly-2132yong-you-piao-tie-man-wang-znigh/" target="_blank">LeetCode题解</a>|
670670
|2140.解决智力问题|中等|<a href="https://leetcode.cn/problems/solving-questions-with-brainpower/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/03/LeetCode%202140.%E8%A7%A3%E5%86%B3%E6%99%BA%E5%8A%9B%E9%97%AE%E9%A2%98/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146991317" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/solving-questions-with-brainpower/solutions/3639601/letmefly-2140jie-jue-zhi-li-wen-ti-ji-yi-pxc4/" target="_blank">LeetCode题解</a>|
671671
|2171.拿出最少数目的魔法豆|中等|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/18/LeetCode%202171.%E6%8B%BF%E5%87%BA%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E9%AD%94%E6%B3%95%E8%B1%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135682601" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/solutions/2609831/letmefly-2171na-chu-zui-shao-shu-mu-de-m-jima/" target="_blank">LeetCode题解</a>|
672+
|2176.统计数组中相等且可以被整除的数对|简单|<a href="https://leetcode.cn/problems/count-equal-and-divisible-pairs-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/17/LeetCode%202176.%E7%BB%9F%E8%AE%A1%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9B%B8%E7%AD%89%E4%B8%94%E5%8F%AF%E4%BB%A5%E8%A2%AB%E6%95%B4%E9%99%A4%E7%9A%84%E6%95%B0%E5%AF%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147315213" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-equal-and-divisible-pairs-in-an-array/solutions/3654492/letmefly-2176tong-ji-shu-zu-zhong-xiang-643mr/" target="_blank">LeetCode题解</a>|
672673
|2178.拆分成最多数目的正偶数之和|中等|<a href="https://leetcode.cn/problems/maximum-split-of-positive-even-integers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/06/LeetCode%202178.%E6%8B%86%E5%88%86%E6%88%90%E6%9C%80%E5%A4%9A%E6%95%B0%E7%9B%AE%E7%9A%84%E6%AD%A3%E5%81%B6%E6%95%B0%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131567568" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-split-of-positive-even-integers/solutions/2332513/letmefly-2178chai-fen-cheng-zui-duo-shu-m44er/" target="_blank">LeetCode题解</a>|
673674
|2180.统计各位数字之和为偶数的整数个数|简单|<a href="https://leetcode.cn/problems/count-integers-with-even-digit-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/06/LeetCode%202180.%E7%BB%9F%E8%AE%A1%E5%90%84%E4%BD%8D%E6%95%B0%E5%AD%97%E4%B9%8B%E5%92%8C%E4%B8%BA%E5%81%B6%E6%95%B0%E7%9A%84%E6%95%B4%E6%95%B0%E4%B8%AA%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128583772" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-integers-with-even-digit-sum/solutions/2048274/letmefly-2180tong-ji-ge-wei-shu-zi-zhi-h-g2ib/" target="_blank">LeetCode题解</a>|
674675
|2187.完成旅途的最少时间|中等|<a href="https://leetcode.cn/problems/minimum-time-to-complete-trips/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/05/LeetCode%202187.%E5%AE%8C%E6%88%90%E6%97%85%E9%80%94%E7%9A%84%E6%9C%80%E5%B0%91%E6%97%B6%E9%97%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142720764" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-time-to-complete-trips/solutions/2940132/letmefly-2187wan-cheng-lu-tu-de-zui-shao-kcpj/" target="_blank">LeetCode题解</a>|
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
---
2+
title: 2176.统计数组中相等且可以被整除的数对:两层遍历模拟
3+
date: 2025-04-17 20:23:19
4+
tags: [题解, LeetCode, 简单, 数组, 遍历, 模拟]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2176.统计数组中相等且可以被整除的数对:两层遍历模拟
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-equal-and-divisible-pairs-in-an-array/](https://leetcode.cn/problems/count-equal-and-divisible-pairs-in-an-array/)
11+
12+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;,请你返回满足&nbsp;<code>0 &lt;= i &lt; j &lt; n</code>&nbsp;,<code>nums[i] == nums[j]</code> 且&nbsp;<code>(i * j)</code>&nbsp;能被&nbsp;<code>k</code>&nbsp;整除的数对&nbsp;<code>(i, j)</code>&nbsp;&nbsp;<strong>数目</strong>&nbsp;。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<pre><b>输入:</b>nums = [3,1,2,2,2,1,3], k = 2
19+
<b>输出:</b>4
20+
<strong>解释:</strong>
21+
总共有 4 对数符合所有要求:
22+
- nums[0] == nums[6] 且 0 * 6 == 0 ,能被 2 整除。
23+
- nums[2] == nums[3] 且 2 * 3 == 6 ,能被 2 整除。
24+
- nums[2] == nums[4] 且 2 * 4 == 8 ,能被 2 整除。
25+
- nums[3] == nums[4] 且 3 * 4 == 12 ,能被 2 整除。
26+
</pre>
27+
28+
<p><strong>示例 2:</strong></p>
29+
30+
<pre><b>输入:</b>nums = [1,2,3,4], k = 1
31+
<b>输出:</b>0
32+
<b>解释:</b>由于数组中没有重复数值,所以没有数对 (i,j) 符合所有要求。
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
37+
<p><strong>提示:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
41+
<li><code>1 &lt;= nums[i], k &lt;= 100</code></li>
42+
</ul>
43+
44+
45+
46+
## 解题方法:模拟
47+
48+
第一层使用变量$i$从$0$到$n-1$遍历,第二层使用变量$j$从$i+1$到$n-1$遍历。
49+
50+
如果$nums[i]==nums[j]$相等且$i\times j \% k==0$,则答案数量加一。
51+
52+
+ 时间复杂度$O(len(nums)^2)$
53+
+ 空间复杂度$O(1)$
54+
55+
### AC代码
56+
57+
#### C++
58+
59+
```cpp
60+
/*
61+
* @Author: LetMeFly
62+
* @Date: 2025-04-17 20:13:54
63+
* @LastEditors: LetMeFly.xyz
64+
* @LastEditTime: 2025-04-17 20:28:40
65+
*/
66+
class Solution {
67+
public:
68+
int countPairs(vector<int>& nums, int k) {
69+
int ans = 0;
70+
for (int i = 0; i < nums.size(); i++) {
71+
for (int j = i + 1; j < nums.size(); j++) {
72+
if (nums[i] == nums[j] && i * j % k == 0) {
73+
ans++;
74+
}
75+
}
76+
}
77+
return ans;
78+
}
79+
};
80+
```
81+
82+
#### Python
83+
84+
```python
85+
'''
86+
Author: LetMeFly
87+
Date: 2025-04-17 20:15:46
88+
LastEditors: LetMeFly.xyz
89+
LastEditTime: 2025-04-17 20:15:58
90+
'''
91+
from typing import List
92+
93+
94+
class Solution:
95+
def countPairs(self, nums: List[int], k: int) -> int:
96+
return sum(nums[i] == nums[j] and i * j % k == 0 for i in range(len(nums)) for j in range(i + 1, len(nums)))
97+
```
98+
99+
#### Java
100+
101+
```java
102+
/*
103+
* @Author: LetMeFly
104+
* @Date: 2025-04-17 20:17:48
105+
* @LastEditors: LetMeFly.xyz
106+
* @LastEditTime: 2025-04-17 20:18:12
107+
*/
108+
class Solution {
109+
public int countPairs(int[] nums, int k) {
110+
int ans = 0;
111+
for (int i = 0; i < nums.length; i++) {
112+
for (int j = i + 1; j < nums.length; j++) {
113+
if (nums[i] == nums[j] && i * j % k == 0) {
114+
ans++;
115+
}
116+
}
117+
}
118+
return ans;
119+
}
120+
}
121+
```
122+
123+
#### Go
124+
125+
```go
126+
/*
127+
* @Author: LetMeFly
128+
* @Date: 2025-04-17 20:19:13
129+
* @LastEditors: LetMeFly.xyz
130+
* @LastEditTime: 2025-04-17 20:21:29
131+
*/
132+
package main
133+
134+
func countPairs(nums []int, k int) (ans int) {
135+
for i := range nums {
136+
for j := i + 1; j < len(nums); j++ {
137+
if nums[i] == nums[j] && i * j % k == 0 {
138+
ans++
139+
}
140+
}
141+
}
142+
return
143+
}
144+
```
145+
146+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147315213)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/17/LeetCode%202176.%E7%BB%9F%E8%AE%A1%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9B%B8%E7%AD%89%E4%B8%94%E5%8F%AF%E4%BB%A5%E8%A2%AB%E6%95%B4%E9%99%A4%E7%9A%84%E6%95%B0%E5%AF%B9/)哦~
147+
>
148+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,8 @@ categories: [自用]
11031103
|mackintosh|n. 防水胶布,胶布雨衣(麦金托什)|
11041104
|||
11051105
|ferrous|adj. 含铁的,铁的|
1106+
|||
1107+
|stammer|v. 口吃,结结巴巴地说<br.>n. 口吃,结巴|
11061108

11071109
<p class="wordCounts">单词收录总数</p>
11081110

newSolution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: LetMeFly
33
Date: 2022-07-03 11:21:14
44
LastEditors: LetMeFly.xyz
5-
LastEditTime: 2025-04-14 00:27:31
5+
LastEditTime: 2025-04-17 20:24:00
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
What's more: 代码结构写的很混乱 - 想单文件实现所有操作
@@ -60,7 +60,7 @@ def getPlatform():
6060
return 'MacOS'
6161
else:
6262
return 'Linux(or others)'
63-
issueTitle = f'Who can add 1 more problem of LeetCode {num}' # (#872)
63+
issueTitle = f'[newSolution]Who can add 1 more problem of LeetCode {num}' # (#872)
6464
alreadyRelatedIssueLists = os.popen(f'gh issue list --search "{issueTitle}"').read()
6565
alreadyRelatedIssueListsSplited = alreadyRelatedIssueLists.split('\n')
6666
print(alreadyRelatedIssueLists)

0 commit comments

Comments
 (0)