Skip to content

Commit 4fe0f74

Browse files
authored
update: 添加问题“3583.统计特殊三元组”的代码和题解 (#1211)
* 3583: WA.cpp (#1210) 0 - re compute * 3583+word: AC.cpp+en (#1210) cpp - AC,95.35%,90.92% before.设备更换前 * update: 添加问题“3583.统计特殊三元组”的代码和题解 (#1211) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 5088eb0 commit 4fe0f74

File tree

8 files changed

+714
-11
lines changed

8 files changed

+714
-11
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-09 18:38:13
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-09 18:43:53
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
const long long MOD = 1e9 + 7;
13+
class Solution {
14+
public:
15+
int specialTriplets(vector<int>& nums) {
16+
ll ans = 0;
17+
unordered_map<int, int> total, now;
18+
for (int t : nums) {
19+
total[t]++;
20+
}
21+
for (int t : nums) {
22+
int finding = t * 2;
23+
if (now.count(finding)) {
24+
ll left = now[finding];
25+
ans = (ans + left * (total[finding] - left - (t == 0))) % MOD;
26+
}
27+
now[t]++;
28+
}
29+
return ans;
30+
}
31+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@
10531053
|3508.设计路由器|中等|<a href="https://leetcode.cn/problems/implement-router/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/20/LeetCode%203508.%E8%AE%BE%E8%AE%A1%E8%B7%AF%E7%94%B1%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151901838" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/implement-router/solutions/3787451/letmefly-3508she-ji-lu-you-qi-stltao-stl-ehkf/" target="_blank">LeetCode题解</a>|
10541054
|3516.找到最近的人|简单|<a href="https://leetcode.cn/problems/find-closest-person/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/04/LeetCode%203516.%E6%89%BE%E5%88%B0%E6%9C%80%E8%BF%91%E7%9A%84%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151184074" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-closest-person/solutions/3772009/letmefly-3516zhao-dao-zui-jin-de-ren-ji-8rlbz/" target="_blank">LeetCode题解</a>|
10551055
|3541.找到频率最高的元音和辅音|简单|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/13/LeetCode%203541.%E6%89%BE%E5%88%B0%E9%A2%91%E7%8E%87%E6%9C%80%E9%AB%98%E7%9A%84%E5%85%83%E9%9F%B3%E5%92%8C%E8%BE%85%E9%9F%B3/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151653999" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/solutions/3780744/letmefly-3541zhao-dao-pin-lu-zui-gao-de-yv5vs/" target="_blank">LeetCode题解</a>|
1056+
|3583.统计特殊三元组|中等|<a href="https://leetcode.cn/problems/count-special-triplets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/09/LeetCode%203583.%E7%BB%9F%E8%AE%A1%E7%89%B9%E6%AE%8A%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155754955" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-special-triplets/solutions/3853381/letmefly-3583tong-ji-te-shu-san-yuan-zu-cdnx3/" target="_blank">LeetCode题解</a>|
10561057
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
10571058
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
10581059
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
title: 3583.统计特殊三元组:哈希表计数
3+
date: 2025-12-09 23:51:33
4+
tags: [题解, LeetCode, 中等, 数组, 哈希表, map, 计数]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3583.统计特殊三元组:哈希表计数
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-special-triplets/](https://leetcode.cn/problems/count-special-triplets/)
11+
12+
<p>给你一个整数数组 <code>nums</code>。</p>
13+
14+
<p><strong>特殊三元组 </strong>定义为满足以下条件的下标三元组 <code>(i, j, k)</code>:</p>
15+
16+
<ul>
17+
<li><code>0 &lt;= i &lt; j &lt; k &lt; n</code>,其中 <code>n = nums.length</code></li>
18+
<li><code>nums[i] == nums[j] * 2</code></li>
19+
<li><code>nums[k] == nums[j] * 2</code></li>
20+
</ul>
21+
22+
<p>返回数组中&nbsp;<strong>特殊三元组&nbsp;</strong>的总数。</p>
23+
24+
<p>由于答案可能非常大,请返回结果对 <code>10<sup>9</sup> + 7</code> 取余数后的值。</p>
25+
26+
<p>&nbsp;</p>
27+
28+
<p><strong class="example">示例 1:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>输入:</strong> <span class="example-io">nums = [6,3,6]</span></p>
32+
33+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
34+
35+
<p><strong>解释:</strong></p>
36+
37+
<p>唯一的特殊三元组是 <code>(i, j, k) = (0, 1, 2)</code>,其中:</p>
38+
39+
<ul>
40+
<li><code>nums[0] = 6</code>, <code>nums[1] = 3</code>, <code>nums[2] = 6</code></li>
41+
<li><code>nums[0] = nums[1] * 2 = 3 * 2 = 6</code></li>
42+
<li><code>nums[2] = nums[1] * 2 = 3 * 2 = 6</code></li>
43+
</ul>
44+
</div>
45+
46+
<p><strong class="example">示例 2:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>输入:</strong> <span class="example-io">nums = [0,1,0,0]</span></p>
50+
51+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
52+
53+
<p><strong>解释:</strong></p>
54+
55+
<p>唯一的特殊三元组是 <code>(i, j, k) = (0, 2, 3)</code>,其中:</p>
56+
57+
<ul>
58+
<li><code>nums[0] = 0</code>, <code>nums[2] = 0</code>, <code>nums[3] = 0</code></li>
59+
<li><code>nums[0] = nums[2] * 2 = 0 * 2 = 0</code></li>
60+
<li><code>nums[3] = nums[2] * 2 = 0 * 2 = 0</code></li>
61+
</ul>
62+
</div>
63+
64+
<p><strong class="example">示例 3:</strong></p>
65+
66+
<div class="example-block">
67+
<p><strong>输入:</strong> <span class="example-io">nums = [8,4,2,8,4]</span></p>
68+
69+
<p><strong>输出:</strong> <span class="example-io">2</span></p>
70+
71+
<p><strong>解释:</strong></p>
72+
73+
<p>共有两个特殊三元组:</p>
74+
75+
<ul>
76+
<li><code>(i, j, k) = (0, 1, 3)</code>
77+
78+
<ul>
79+
<li><code>nums[0] = 8</code>, <code>nums[1] = 4</code>, <code>nums[3] = 8</code></li>
80+
<li><code>nums[0] = nums[1] * 2 = 4 * 2 = 8</code></li>
81+
<li><code>nums[3] = nums[1] * 2 = 4 * 2 = 8</code></li>
82+
</ul>
83+
</li>
84+
<li><code>(i, j, k) = (1, 2, 4)</code>
85+
<ul>
86+
<li><code>nums[1] = 4</code>, <code>nums[2] = 2</code>, <code>nums[4] = 4</code></li>
87+
<li><code>nums[1] = nums[2] * 2 = 2 * 2 = 4</code></li>
88+
<li><code>nums[4] = nums[2] * 2 = 2 * 2 = 4</code></li>
89+
</ul>
90+
</li>
91+
</ul>
92+
</div>
93+
94+
<p>&nbsp;</p>
95+
96+
<p><strong>提示:</strong></p>
97+
98+
<ul>
99+
<li><code>3 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
100+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
101+
</ul>
102+
103+
104+
105+
## 解题方法:哈希表计数
106+
107+
使用一个哈希表,统计nums中每个数分别出现了多少次。
108+
109+
接着再次遍历nums数组,再使用一个哈希表,统计到目前为止每个数分别出现了多少次。
110+
111+
假设当前遍历元素为$t$,那么在此之前$2t$出现的次数乘以$2t$在此之后出现的次数($出现总次数-此前出现次数$)累加到答案中。
112+
113+
注意:
114+
115+
1. 特判$t=0$时
116+
2. 其实也可以使用64位整数并且只在最后取一次模
117+
118+
### 时空复杂度分析
119+
120+
+ 时间复杂度$O(len(nums))$
121+
+ 空间复杂度$O(len(nums))$
122+
123+
### AC代码
124+
125+
#### C++
126+
127+
```cpp
128+
/*
129+
* @LastEditTime: 2025-12-09 18:43:53
130+
*/
131+
typedef long long ll;
132+
const long long MOD = 1e9 + 7;
133+
class Solution {
134+
public:
135+
int specialTriplets(vector<int>& nums) {
136+
ll ans = 0;
137+
unordered_map<int, int> total, now;
138+
for (int t : nums) {
139+
total[t]++;
140+
}
141+
for (int t : nums) {
142+
int finding = t * 2;
143+
if (now.count(finding)) {
144+
ll left = now[finding];
145+
ans = (ans + left * (total[finding] - left - (t == 0))) % MOD;
146+
}
147+
now[t]++;
148+
}
149+
return ans;
150+
}
151+
};
152+
```
153+
154+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/155754955)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/09/LeetCode%203583.%E7%BB%9F%E8%AE%A1%E7%89%B9%E6%AE%8A%E4%B8%89%E5%85%83%E7%BB%84/)哦~
155+
>
156+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,9 @@ categories: [自用]
16551655
|irrigate|v. 灌溉,冲洗(伤口/身体部位)|
16561656
|heir|n. 继承人|
16571657
|bona fide|adj. 真诚的,真实的,合法的|
1658+
|||
1659+
|puff|v. 吸(烟);喷出,冒出(烟);喘气<br/>n. 一小口(烟),一缕,少量(烟/气);泡芙;吹捧的文章|
1660+
|oblige|v. (以法律/义务等)强迫;帮忙,效劳;使感激;使成为必要<br/>phrase. (feel obliged to do sth.)觉得有义务做;不得不做|
16581661

16591662
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
16601663
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

chat.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-11-25 11:19:58
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-12-09 23:17:49
6+
'''
7+
"""
8+
9+
我想自动化LeetCode的那个项目(https://github.com/LetMeFly666/LeetCode),当前最想自动化的是每日一题。
10+
关于每日一题我有一个标记是从2025.4.7日开始的,这之后每天都会创建一个issue,标题格式为“[newSolution]Who can add 1 more problem of LeetCode 2873”或者部分不带“[newSolution]”的“Who can add 1 more problem of LeetCode 2873”。我想实现以下内容:
11+
- 将不带“[newSolution]”的相关issue加上这个标题(注意是4.7日及之后,注意是Who can add 1 more problem of xx的issue),希望可以先列举并等我确认后再执行rename
12+
- 统一重命名之后,将4.7日之后到今天为止的所有相关issue添加到https://github.com/users/LetMeFly666/projects/5/views/1
13+
- 之后每天我都想创建一个issue,内容为今天的每日一题(这可能涉及到爬虫,如果你没有较好的或现有的爬虫方案,可以先留下接口等我后续分析和实现)
14+
- 之后所有[newSolution]Who can add 1 more problem of LeetCode xxxx 为标题的issue全部自动添加到projects/5/views/1中
15+
- 如果有pr关联了这个issue,则标记状态为In Progress; 如果有issue带有“solving”标签,则标记状态为In Progress(去掉solving标签也要相应地标记回TODO状态
16+
17+
额外信息:
18+
- 整个内容可以以任何自动化方式实现,例如归一化issueName也可以单独写一个脚本在本地运行一次
19+
- 4.7日之前也有类似issue,暂且不在本次统计范围内
20+
- 你可以使用任何方法实现,包括但不限于一个或多个workflow,或部分本地执行的gh命令或python脚本,但本地执行的只能今天执行一次,创建好后要依靠自动执行
21+
- 所有符合条件的issue都带有“题解”label
22+
23+
24+
25+
是的,回答你的问题:
26+
①是
27+
②提供一个现成的 LeetCode 每日一题爬取方案
28+
③已创建
29+
④是,或者“ close: #1204”也算
30+
⑤全部
31+
32+
额外补充:
33+
- 每日自动创建每日一题是UTC+8的0:10
34+
- issue的body内容为:“By xxx | 3583.统计特殊三元组”,其中xxx为创建脚本名称和链接,链接要链接到对应的hash地址;其中“3583.统计特殊三元组”也是个链接,要链接到这道题的地址
35+
36+
37+
38+
39+
40+
现在我使用的是Windows,能否将今天运行的那些修改为python脚本呢
41+
42+
43+
44+
请使用gh吧,我Windows系统上也有gh并且gh鉴权过了,不想再申请GITHUB_TOKEN了
45+
46+
47+
我希望你给我返回的是今天的预处理脚本SOS,不是每天运行的脚本
48+
这样,现在你先给我返回今天要运行的脚本,包括rename issue和添加到project
49+
50+
51+
52+
53+
54+
很棒,只是顺序不太正确,毕竟我创建issue时候的顺序就不太正确,这不能怪你。
55+
我的每日一题正确顺序如下,请你在targets中按照此顺序排序,若不在此顺序列表中,则排序到最后。
56+
返回需要修改的代码。
57+
此外,我刚刚新增了字段Date,类型为日期,请从“ 2873 #882”开始依次填入日期字段
58+
59+
60+
61+
62+
63+
64+
这样匹配有很大的匹配失败风险,以LeetCode后面的1208为依据排序
65+
66+
67+
68+
69+
不是这个意思嗷,
70+
daily_order = [
71+
"[newSolution]Who can add 1 more problem of LeetCode 2873 #882",
72+
"[newSolution]Who can add 1 more problem of LeetCode 2874 #883",
73+
...
74+
]
75+
76+
以daily_order中的顺序为依据将targets排序,但targets和daily_order中元素相同的依据为:LeetCode后的数字相同
77+
78+
"""

r2.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)