Skip to content

Commit 7d4b060

Browse files
authored
Merge pull request #696 from LetMeFly666/2264
添加问题“2264.字符串中最大的3位相同数字”的代码和题解
2 parents 0a60fbd + b79cdab commit 7d4b060

11 files changed

+482
-11
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-08 15:25:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-08 15:33:22
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
#ifdef FirstTry // WA - 必须是子字符串
12+
class Solution {
13+
public:
14+
string largestGoodInteger(string num) {
15+
int bin[10] = {0};
16+
for (char c : num) {
17+
bin[c - '0']++;
18+
}
19+
for (int i = 9; i >= 0; i--) {
20+
if (bin[i] >= 3) {
21+
return to_string(i) + to_string(i) + to_string(i);
22+
}
23+
}
24+
return "";
25+
}
26+
};
27+
#else // FirstTry
28+
// SecondTry
29+
class Solution {
30+
public:
31+
string largestGoodInteger(string& num) {
32+
char M = '/'; // ASCII在0前一个
33+
for (int i = 2; i < num.size(); i++) {
34+
if (num[i] == num[i - 1] && num[i] == num[i - 2]) {
35+
M = max(M, num[i]);
36+
}
37+
}
38+
return M == '/' ? string() : string(3, M);
39+
}
40+
};
41+
#endif // FirstTry
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-08 15:43:44
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-08 15:46:23
6+
*/
7+
package main
8+
9+
func largestGoodInteger(num string) string {
10+
M := byte('/')
11+
for i := 2; i < len(num); i++ {
12+
if num[i] > M && num[i] == num[i - 1] && num[i] == num[i - 2] {
13+
M = num[i]
14+
}
15+
}
16+
if M == '/' {
17+
return ""
18+
}
19+
return string(M) + string(M) + string(M)
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-08 15:37:06
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-08 15:41:37
6+
*/
7+
class Solution {
8+
public String largestGoodInteger(String num) {
9+
char M = '/';
10+
for (int i = 2; i < num.length(); i++) {
11+
if (num.charAt(i) == num.charAt(i - 1) && num.charAt(i) == num.charAt(i - 2)) {
12+
M = (char) Math.max(M, num.charAt(i));
13+
}
14+
}
15+
return M == '/' ? "" : "" + M + M + M;
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-01-08 15:34:27
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-01-08 15:36:15
6+
'''
7+
class Solution:
8+
def largestGoodInteger(self, s: str) -> str:
9+
M = '/'
10+
for i in range(2, len(s)):
11+
if s[i] == s[i - 1] == s[i - 2]:
12+
M = max(M, s[i])
13+
return '' if M == '/' else M * 3

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@
538538
|2244.完成所有任务需要的最少轮数|中等|<a href="https://leetcode.cn/problems/minimum-rounds-to-complete-all-tasks/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/14/LeetCode%202244.%E5%AE%8C%E6%88%90%E6%89%80%E6%9C%89%E4%BB%BB%E5%8A%A1%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E5%B0%91%E8%BD%AE%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138849002" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-rounds-to-complete-all-tasks/solutions/2777174/letmefly-2244wan-cheng-suo-you-ren-wu-xu-n7bh/" target="_blank">LeetCode题解</a>|
539539
|2251.花期内花的数目|困难|<a href="https://leetcode.cn/problems/number-of-flowers-in-full-bloom/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/09/28/LeetCode%202251.%E8%8A%B1%E6%9C%9F%E5%86%85%E8%8A%B1%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133378624" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-flowers-in-full-bloom/solutions/2462220/letmefly-2251hua-qi-nei-hua-de-shu-mu-pa-nfvo/" target="_blank">LeetCode题解</a>|
540540
|2258.逃离火灾|困难|<a href="https://leetcode.cn/problems/escape-the-spreading-fire/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/09/LeetCode%202258.%E9%80%83%E7%A6%BB%E7%81%AB%E7%81%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134331955" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/escape-the-spreading-fire/solutions/2520786/letmefly-2258tao-chi-huo-zai-yan-du-you-kj2r4/" target="_blank">LeetCode题解</a>|
541+
|2264.字符串中最大的3位相同数字|简单|<a href="https://leetcode.cn/problems/largest-3-same-digit-number-in-string/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/08/LeetCode%202264.%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%9C%80%E5%A4%A7%E7%9A%843%E4%BD%8D%E7%9B%B8%E5%90%8C%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145011261" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-3-same-digit-number-in-string/solutions/3042054/letmefly-2264zi-fu-chuan-zhong-zui-da-de-5vja/" target="_blank">LeetCode题解</a>|
541542
|2274.不含特殊楼层的最大连续楼层数|中等|<a href="https://leetcode.cn/problems/maximum-consecutive-floors-without-special-floors/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/06/LeetCode%202274.%E4%B8%8D%E5%90%AB%E7%89%B9%E6%AE%8A%E6%A5%BC%E5%B1%82%E7%9A%84%E6%9C%80%E5%A4%A7%E8%BF%9E%E7%BB%AD%E6%A5%BC%E5%B1%82%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144972086" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-consecutive-floors-without-special-floors/solutions/3040396/letmefly-2274bu-han-te-shu-lou-ceng-de-z-321m/" target="_blank">LeetCode题解</a>|
542543
|2276.统计区间中的整数数目|困难|<a href="https://leetcode.cn/problems/count-integers-in-intervals/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/16/LeetCode%202276.%E7%BB%9F%E8%AE%A1%E5%8C%BA%E9%97%B4%E4%B8%AD%E7%9A%84%E6%95%B4%E6%95%B0%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135036679" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-integers-in-intervals/solutions/2568803/letmefly-2276tong-ji-qu-jian-zhong-de-zh-y3i6/" target="_blank">LeetCode题解</a>|
543544
|2283.判断一个数的数字计数是否等于数位的值|简单|<a href="https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/11/LeetCode%202283.%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%95%B0%E7%9A%84%E6%95%B0%E5%AD%97%E8%AE%A1%E6%95%B0%E6%98%AF%E5%90%A6%E7%AD%89%E4%BA%8E%E6%95%B0%E4%BD%8D%E7%9A%84%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128652351" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/solutions/2057234/letmefly-2283pan-duan-yi-ge-shu-de-shu-z-4olo/" target="_blank">LeetCode题解</a>|
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
title: 2264.字符串中最大的 3 位相同数字
3+
date: 2025-01-08 15:48:37
4+
tags: [题解, LeetCode, 简单, 字符串]
5+
---
6+
7+
# 【LetMeFly】2264.字符串中最大的 3 位相同数字:遍历
8+
9+
力扣题目链接:[https://leetcode.cn/problems/largest-3-same-digit-number-in-string/](https://leetcode.cn/problems/largest-3-same-digit-number-in-string/)
10+
11+
<p>给你一个字符串 <code>num</code> ,表示一个大整数。如果一个整数满足下述所有条件,则认为该整数是一个 <strong>优质整数</strong> :</p>
12+
13+
<ul>
14+
<li>该整数是 <code>num</code> 的一个长度为 <code>3</code> 的 <strong>子字符串</strong> 。</li>
15+
<li>该整数由唯一一个数字重复 <code>3</code> 次组成。</li>
16+
</ul>
17+
18+
<p>以字符串形式返回 <strong>最大的优质整数</strong> 。如果不存在满足要求的整数,则返回一个空字符串 <code>""</code> 。</p>
19+
20+
<p><strong>注意:</strong></p>
21+
22+
<ul>
23+
<li><strong>子字符串</strong> 是字符串中的一个连续字符序列。</li>
24+
<li><code>num</code> 或优质整数中可能存在 <strong>前导零</strong> 。</li>
25+
</ul>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>示例 1:</strong></p>
30+
31+
<pre>
32+
<strong>输入:</strong>num = "6<em><strong>777</strong></em>133339"
33+
<strong>输出:</strong>"777"
34+
<strong>解释:</strong>num 中存在两个优质整数:"777" 和 "333" 。
35+
"777" 是最大的那个,所以返回 "777" 。
36+
</pre>
37+
38+
<p><strong>示例 2:</strong></p>
39+
40+
<pre>
41+
<strong>输入:</strong>num = "23<em><strong>000</strong></em>19"
42+
<strong>输出:</strong>"000"
43+
<strong>解释:</strong>"000" 是唯一一个优质整数。
44+
</pre>
45+
46+
<p><strong>示例 3:</strong></p>
47+
48+
<pre>
49+
<strong>输入:</strong>num = "42352338"
50+
<strong>输出:</strong>""
51+
<strong>解释:</strong>不存在长度为 3 且仅由一个唯一数字组成的整数。因此,不存在优质整数。
52+
</pre>
53+
54+
<p>&nbsp;</p>
55+
56+
<p><strong>提示:</strong></p>
57+
58+
<ul>
59+
<li><code>3 &lt;= num.length &lt;= 1000</code></li>
60+
<li><code>num</code> 仅由数字(<code>0</code> - <code>9</code>)组成</li>
61+
</ul>
62+
63+
64+
65+
## 解题方法:遍历
66+
67+
使用一个变量记录最大的优质整数的字符,遍历字符串并不断更新这个最大值即可。
68+
69+
+ 时间复杂度$O(len(num))$
70+
+ 空间复杂度$O(1)$
71+
72+
### AC代码
73+
74+
#### C++
75+
76+
```cpp
77+
/*
78+
* @Author: LetMeFly
79+
* @Date: 2025-01-08 15:25:00
80+
* @LastEditors: LetMeFly.xyz
81+
* @LastEditTime: 2025-01-08 15:33:22
82+
*/
83+
class Solution {
84+
public:
85+
string largestGoodInteger(string& num) {
86+
char M = '/'; // ASCII在0前一个
87+
for (int i = 2; i < num.size(); i++) {
88+
if (num[i] == num[i - 1] && num[i] == num[i - 2]) {
89+
M = max(M, num[i]);
90+
}
91+
}
92+
return M == '/' ? string() : string(3, M);
93+
}
94+
};
95+
```
96+
97+
#### Python
98+
99+
```python
100+
'''
101+
Author: LetMeFly
102+
Date: 2025-01-08 15:34:27
103+
LastEditors: LetMeFly.xyz
104+
LastEditTime: 2025-01-08 15:36:15
105+
'''
106+
class Solution:
107+
def largestGoodInteger(self, s: str) -> str:
108+
M = '/'
109+
for i in range(2, len(s)):
110+
if s[i] == s[i - 1] == s[i - 2]:
111+
M = max(M, s[i])
112+
return '' if M == '/' else M * 3
113+
```
114+
115+
#### Java
116+
117+
```java
118+
/*
119+
* @Author: LetMeFly
120+
* @Date: 2025-01-08 15:37:06
121+
* @LastEditors: LetMeFly.xyz
122+
* @LastEditTime: 2025-01-08 15:41:37
123+
*/
124+
class Solution {
125+
public String largestGoodInteger(String num) {
126+
char M = '/';
127+
for (int i = 2; i < num.length(); i++) {
128+
if (num.charAt(i) == num.charAt(i - 1) && num.charAt(i) == num.charAt(i - 2)) {
129+
M = (char) Math.max(M, num.charAt(i));
130+
}
131+
}
132+
return M == '/' ? "" : "" + M + M + M;
133+
}
134+
}
135+
```
136+
137+
#### Go
138+
139+
```go
140+
/*
141+
* @Author: LetMeFly
142+
* @Date: 2025-01-08 15:43:44
143+
* @LastEditors: LetMeFly.xyz
144+
* @LastEditTime: 2025-01-08 15:46:23
145+
*/
146+
package main
147+
148+
func largestGoodInteger(num string) string {
149+
M := byte('/')
150+
for i := 2; i < len(num); i++ {
151+
if num[i] > M && num[i] == num[i - 1] && num[i] == num[i - 2] {
152+
M = num[i]
153+
}
154+
}
155+
if M == '/' {
156+
return ""
157+
}
158+
return string(M) + string(M) + string(M)
159+
}
160+
```
161+
162+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/01/08/LeetCode%202264.%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%9C%80%E5%A4%A7%E7%9A%843%E4%BD%8D%E7%9B%B8%E5%90%8C%E6%95%B0%E5%AD%97/)哦~
163+
>
164+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/145011261](https://letmefly.blog.csdn.net/article/details/145011261)

api/calendar/README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2024-12-15 16:10:07
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-01-05 13:38:33
5+
* @LastEditTime: 2025-01-07 23:43:43
66
-->
77
# 目的
88

@@ -27,6 +27,9 @@
2727
# TODO
2828

2929
- [ ] 删除标签时,前端提醒“所有任务的xx标签将会被移除”
30+
- [ ] 前端,创建完一个事件后创建另一个事件时内容不清空
31+
- [ ] 前端,默认显示时间是UTC时间而不是本地时间
32+
- [ ] 后端,taskTag数据库中删除一条记录时,这个taskId的所有taskTag记录都会被删除
3033
- [ ] 前端,事件修改 - 这个可以全部删了重建
3134
- [ ] 后端 - 事件创建 - 时间覆盖重叠问题
3235
- [ ] fix: 前端 - 拖拽后默认时间不正确
@@ -878,13 +881,61 @@ html表格,设置所有列等宽。
878881
创建事件时,可以输入事件标题、事件描述,默认依据用户的拖拽范围给定一个起止时间,用户也可以调整起止时间。
879882

880883
<hr/>
884+
885+
我有一个Calendar_Tasks数据表,里面有task信息和userid信息;
886+
我有一个Calendar_TaskTag数据表,里面有taskId和tagId的对应信息。
887+
888+
我想通过userid查询出这个user的所有任务,以及每个任务对应的tagId。我应该如何查询?
889+
890+
```
891+
```
892+
881893
<hr/>
894+
895+
解释left join
896+
882897
<hr/>
898+
899+
这样查询的效率如何
900+
883901
<hr/>
902+
903+
left join的时间复杂度是多少?
904+
884905
<hr/>
906+
907+
这样左表中的信息是不是会一个tag重复一次
908+
885909
<hr/>
910+
911+
GROUP_CONCAT这个好,请详细解释之
912+
886913
<hr/>
914+
915+
如果task表列比较多的话,可否Select task.*
916+
887917
<hr/>
918+
919+
这样会每个userid都关联一次,我可用做到只管理指定userid的task吗?
920+
921+
我尝试如下代码报错了:
922+
923+
```
924+
SELECT
925+
Calendar_Tasks.*,
926+
GROUP_CONCAT(Calendar_TaskTag.tagId) as tagIds
927+
FROM
928+
Calendar_Tasks
929+
LEFT JOIN
930+
Calendar_TaskTag
931+
ON
932+
Calendar_Tasks.taskId = Calendar_TaskTag.taskId
933+
GROUP BY
934+
Calendar_Tasks.taskId
935+
WHERE
936+
Calendar_Tasks.userid = 1
937+
```
938+
888939
<hr/>
889940
<hr/>
890941
<hr/>

api/calendar/back/README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2024-12-20 22:12:40
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2024-12-24 10:43:09
5+
* @LastEditTime: 2025-01-07 23:47:31
66
-->
77
# calendar后端
88

@@ -85,15 +85,17 @@ None。 TODO: 起止时间
8585
"description": "Let Calendar开发",
8686
"startTime": "2024-12-17T10:00:00",
8787
"during": 60,
88-
"userid": 1
88+
"userid": 1,
89+
"tagIds": null
8990
},
9091
{
91-
"taskId": 13,
92-
"title": "开发",
93-
"description": "LetCalendar开发",
94-
"startTime": "2024-12-20T10:00:00",
95-
"during": 60,
96-
"userid": 1
92+
"taskId": 18,
93+
"title": "聊天",
94+
"description": "哄对象不生气[trumble]",
95+
"startTime": "2025-01-07T12:20:00",
96+
"during": 180,
97+
"userid": 1,
98+
"tagIds": "5,7"
9799
}
98100
]
99101
```

0 commit comments

Comments
 (0)