Skip to content

Commit 5fb5f9e

Browse files
LetMeFly666Copilot
andauthored
update: 添加问题“3043.最长公共前缀的长度”的代码和题解 + feat/settings.json(files.associations删除不必要部分+先默认关AI补全) (#1601)
* 3043: tmp (#1600) - 尝试禁用仅Codes目录下的AI补全 - 失败 * feat: 先全关,后面写脚本Coding状态下自动关闭结束后自动打开好了 (#1600) + remove many cpp header files associations * 3043: AC.cpp (#1600) - AC,56.14%,84.21% * update: 添加问题“3043.最长公共前缀的长度”的代码和题解 (#1601) Signed-off-by: LetMeFly666 <Tisfy@qq.com> * fix: Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * feat: files.associations删除不必要部分 --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4f2c31d commit 5fb5f9e

6 files changed

Lines changed: 161 additions & 114 deletions

File tree

.vscode/settings.json

Lines changed: 4 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,16 @@
11
{
22
"files.associations": {
3-
"*.pro": "plaintext",
43
"*.wxss": "css",
54
"*.wxml": "html",
65
".commitmsg": "git-commit",
7-
".commitTitleExtra": "git-commit",
8-
"array": "cpp",
9-
"atomic": "cpp",
10-
"*.tcc": "cpp",
11-
"bitset": "cpp",
12-
"cctype": "cpp",
13-
"cfenv": "cpp",
14-
"chrono": "cpp",
15-
"cinttypes": "cpp",
16-
"clocale": "cpp",
17-
"cmath": "cpp",
18-
"complex": "cpp",
19-
"condition_variable": "cpp",
20-
"csetjmp": "cpp",
21-
"csignal": "cpp",
22-
"cstdarg": "cpp",
23-
"cstddef": "cpp",
24-
"cstdint": "cpp",
25-
"cstdio": "cpp",
26-
"cstdlib": "cpp",
27-
"cstring": "cpp",
28-
"ctime": "cpp",
29-
"cwchar": "cpp",
30-
"cwctype": "cpp",
31-
"deque": "cpp",
32-
"forward_list": "cpp",
33-
"list": "cpp",
34-
"unordered_map": "cpp",
35-
"unordered_set": "cpp",
36-
"vector": "cpp",
37-
"exception": "cpp",
38-
"fstream": "cpp",
39-
"functional": "cpp",
40-
"future": "cpp",
41-
"initializer_list": "cpp",
42-
"iomanip": "cpp",
43-
"iosfwd": "cpp",
44-
"iostream": "cpp",
45-
"istream": "cpp",
46-
"limits": "cpp",
47-
"mutex": "cpp",
48-
"new": "cpp",
49-
"ostream": "cpp",
50-
"numeric": "cpp",
51-
"ratio": "cpp",
52-
"scoped_allocator": "cpp",
53-
"sstream": "cpp",
54-
"stdexcept": "cpp",
55-
"streambuf": "cpp",
56-
"system_error": "cpp",
57-
"thread": "cpp",
58-
"regex": "cpp",
59-
"tuple": "cpp",
60-
"type_traits": "cpp",
61-
"utility": "cpp",
62-
"typeindex": "cpp",
63-
"typeinfo": "cpp",
64-
"valarray": "cpp",
65-
"random": "cpp",
66-
"map": "cpp",
67-
"string": "cpp",
68-
"algorithm": "cpp",
69-
"cassert": "cpp",
70-
"cerrno": "cpp",
71-
"cfloat": "cpp",
72-
"ciso646": "cpp",
73-
"climits": "cpp",
74-
"stack": "cpp",
75-
"xstring": "cpp",
76-
"shared_mutex": "cpp",
77-
"string_view": "cpp",
78-
"locale": "cpp",
79-
"stdio.h": "c",
80-
"windows.h": "c",
81-
"optional": "cpp",
82-
"charconv": "cpp",
83-
"codecvt": "cpp",
84-
"cuchar": "cpp",
85-
"iterator": "cpp",
86-
"memory": "cpp",
87-
"memory_resource": "cpp",
88-
"set": "cpp",
89-
"math.h": "c",
90-
"__hash_table": "cpp",
91-
"__split_buffer": "cpp",
92-
"__tree": "cpp",
93-
"queue": "cpp",
94-
"span": "cpp",
95-
"__locale": "cpp",
96-
"ios": "cpp",
97-
"__config": "cpp",
98-
"__bit_reference": "cpp",
99-
"any": "cpp",
100-
"barrier": "cpp",
101-
"latch": "cpp",
102-
"numbers": "cpp",
103-
"semaphore": "cpp",
104-
"variant": "cpp",
105-
"__node_handle": "cpp",
106-
"__verbose_abort": "cpp",
107-
"execution": "cpp",
108-
"print": "cpp",
109-
"filesystem": "cpp",
110-
"format": "cpp",
111-
"ccomplex": "cpp",
112-
"cstdbool": "cpp",
113-
"ctgmath": "cpp",
114-
"__assert": "cpp",
115-
"ranges": "cpp",
116-
"version": "cpp",
117-
"bit": "cpp"
6+
".commitTitleExtra": "git-commit"
1187
},
1198
"files.watcherExclude": {
1209
"**/AllProblems/**": true
12110
},
12211
"editor.mouseWheelZoom": true,
12312
"workbench.tree.enableStickyScroll": true,
13+
"github.copilot.enable": {
14+
"*": false
15+
}
12416
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-05-21 22:00:11
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-05-21 22:32:19
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
int len(int t) {
14+
int ans = 0;
15+
while (t) {
16+
ans++;
17+
t /= 10;
18+
}
19+
return ans;
20+
}
21+
public:
22+
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
23+
unordered_set<int> can;
24+
for (int t : arr1) {
25+
while (t) {
26+
can.insert(t);
27+
t /= 10;
28+
}
29+
}
30+
31+
int ans = 0;
32+
for (int t : arr2) {
33+
while (t) {
34+
if (can.count(t)) {
35+
ans = max(ans, len(t));
36+
break;
37+
}
38+
t /= 10;
39+
}
40+
}
41+
return ans;
42+
}
43+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2022-05-19 18:48:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-05-17 11:48:18
5+
* @LastEditTime: 2026-05-21 22:33:23
66
-->
77
# LetLeet Blog
88

@@ -1037,6 +1037,7 @@
10371037
|3027.人员站位的方案数II|困难|<a href="https://leetcode.cn/problems/find-the-number-of-ways-to-place-people-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/05/LeetCode%203027.%E4%BA%BA%E5%91%98%E7%AB%99%E4%BD%8D%E7%9A%84%E6%96%B9%E6%A1%88%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151220862" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-number-of-ways-to-place-people-ii/solutions/3772911/letmefly-3027ren-yuan-zhan-wei-de-fang-a-03g4/" target="_blank">LeetCode题解</a>|
10381038
|3033.修改矩阵|简单|<a href="https://leetcode.cn/problems/modify-the-matrix/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/05/LeetCode%203033.%E4%BF%AE%E6%94%B9%E7%9F%A9%E9%98%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140219034" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/modify-the-matrix/solutions/2832430/letmefly-3033xiu-gai-ju-zhen-yuan-di-xiu-e0cy/" target="_blank">LeetCode题解</a>|
10391039
|3038.相同分数的最大操作数目I|简单|<a href="https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/07/LeetCode%203038.%E7%9B%B8%E5%90%8C%E5%88%86%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E6%93%8D%E4%BD%9C%E6%95%B0%E7%9B%AEI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139535702" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-i/solutions/2804245/letmefly-3038xiang-tong-fen-shu-de-zui-d-dkj7/" target="_blank">LeetCode题解</a>|
1040+
|3043.最长公共前缀的长度|中等|<a href="https://leetcode.cn/problems/find-the-length-of-the-longest-common-prefix/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/05/21/LeetCode%203043.%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%89%8D%E7%BC%80%E7%9A%84%E9%95%BF%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/161294062" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-length-of-the-longest-common-prefix/solutions/3972278/letmefly-3043zui-chang-gong-gong-qian-zh-qiry/" target="_blank">LeetCode题解</a>|
10401041
|3046.分割数组|简单|<a href="https://leetcode.cn/problems/split-the-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/28/LeetCode%203046.%E5%88%86%E5%89%B2%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144789679" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-the-array/solutions/3032725/letmefly-3046fen-ge-shu-zu-ji-shu-by-tis-33vv/" target="_blank">LeetCode题解</a>|
10411042
|3047.求交集区域内的最大正方形面积|中等|<a href="https://leetcode.cn/problems/find-the-largest-area-of-square-inside-two-rectangles/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/17/LeetCode%203047.%E6%B1%82%E4%BA%A4%E9%9B%86%E5%8C%BA%E5%9F%9F%E5%86%85%E7%9A%84%E6%9C%80%E5%A4%A7%E6%AD%A3%E6%96%B9%E5%BD%A2%E9%9D%A2%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157067926" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-largest-area-of-square-inside-two-rectangles/solutions/3882936/letmefly-3047qiu-jiao-ji-qu-yu-nei-de-zu-2lgs/" target="_blank">LeetCode题解</a>|
10421043
|3065.超过阈值的最少操作数I|简单|<a href="https://leetcode.cn/problems/minimum-operations-to-exceed-threshold-value-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/14/LeetCode%203065.%E8%B6%85%E8%BF%87%E9%98%88%E5%80%BC%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%95%B0I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145136167" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-exceed-threshold-value-i/solutions/3046964/letmefly-3065chao-guo-yu-zhi-de-zui-shao-58ii/" target="_blank">LeetCode题解</a>|
@@ -1269,6 +1270,7 @@
12691270
- [ ] pr预编译机制 - 当有pr时候提前编译好(还能看),merge后如果可行直接使用编译好的内容
12701271
- [ ] 分支名匹配机制/project status更早set2todo机制 - 新分支push时候就能标记一个issue为in progress了,甚至hello的时候就可以
12711272
- [ ] code-server 浏览器运行时候目前没法做到自动修改剪贴板AC百分比
1273+
- [ ] newSolution.py状态机、题解时候支持上下选或指令操作
12721274
- hexo我是一刻也待不下去了
12731275
- [x] 生成题解的时候还是按一下回车再开始吧,要不然想要像[这次](https://github.com/LetMeFly666/LeetCode/issues/787)多次边coding边提交,生成题解文件过早还得手动复制代码过去。
12741276
- [x] 写新题解时,若master本地为最新而远端并非最新,采用squash方式更新时,则远端pr会将本地master的一些commit也压缩为一个,远端和本地就冲突了。(还是保持master远端实时最新吧[#790](https://github.com/LetMeFly666/LeetCode/issues/790))
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: 3043.最长公共前缀的长度:哈希表(不转string)
3+
date: 2026-05-21 22:34:26
4+
tags: [题解, LeetCode, 中等, 字典树, 数组, 哈希表, set, 字符串]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3043.最长公共前缀的长度:哈希表(不转string)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/find-the-length-of-the-longest-common-prefix/](https://leetcode.cn/problems/find-the-length-of-the-longest-common-prefix/)
11+
12+
<p>给你两个 <strong>正整数 </strong>数组 <code>arr1</code> 和 <code>arr2</code> 。</p>
13+
14+
<p>正整数的 <strong>前缀 </strong>是其 <strong>最左边 </strong>的一位或多位数字组成的整数。例如,<code>123</code> 是整数 <code>12345</code> 的前缀,而 <code>234</code><strong> 不是 </strong>。</p>
15+
16+
<p>设若整数 <code>c</code> 是整数 <code>a</code> 和 <code>b</code> 的<strong> 公共前缀 </strong>,那么 <code>c</code> 需要同时是 <code>a</code> 和 <code>b</code> 的前缀。例如,<code>5655359</code> 和 <code>56554</code> 有公共前缀 <code>565</code>&nbsp;和 <code>5655</code>,而 <code>1223</code> 和 <code>43456</code><strong> 没有 </strong>公共前缀。</p>
17+
18+
<p>你需要找出属于 <code>arr1</code> 的整数 <code>x</code> 和属于 <code>arr2</code> 的整数 <code>y</code> 组成的所有数对 <code>(x, y)</code> 之中最长的公共前缀的长度。</p>
19+
20+
<p>返回所有数对之中最长公共前缀的长度。如果它们之间不存在公共前缀,则返回 <code>0</code> 。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong class="example">示例 1:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>arr1 = [1,10,100], arr2 = [1000]
28+
<strong>输出:</strong>3
29+
<strong>解释:</strong>存在 3 个数对 (arr1[i], arr2[j]) :
30+
- (1, 1000) 的最长公共前缀是 1 。
31+
- (10, 1000) 的最长公共前缀是 10 。
32+
- (100, 1000) 的最长公共前缀是 100 。
33+
最长的公共前缀是 100 ,长度为 3 。
34+
</pre>
35+
36+
<p><strong class="example">示例 2:</strong></p>
37+
38+
<pre>
39+
<strong>输入:</strong>arr1 = [1,2,3], arr2 = [4,4,4]
40+
<strong>输出:</strong>0
41+
<strong>解释:</strong>任何数对 (arr1[i], arr2[j]) 之中都不存在公共前缀,因此返回 0 。
42+
请注意,同一个数组内元素之间的公共前缀不在考虑范围内。
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= arr1.length, arr2.length &lt;= 5 * 10<sup>4</sup></code></li>
51+
<li><code>1 &lt;= arr1[i], arr2[i] &lt;= 10<sup>8</sup></code></li>
52+
</ul>
53+
54+
55+
56+
## 解题方法:哈希表
57+
58+
两个数的所有前缀可以相互匹配,所有就把一个数组中每个数的所有前缀放到一个set哈希表中不就好了(不断插入哈希表之后除以10并向下取整)。之后遍历另一个数组,看这个数组中每个数的每个前缀是否在哈希表中出现过,出现则匹配成功。
59+
60+
+ 时间复杂度$O((len(arr1)+len(arr2))\times \log arr[i])$
61+
+ 空间复杂度$O(len(arr1)\log arr1[i])$
62+
63+
### AC代码
64+
65+
#### C++
66+
67+
```cpp
68+
/*
69+
* @LastEditTime: 2026-05-21 22:32:19
70+
*/
71+
class Solution {
72+
private:
73+
int len(int t) {
74+
int ans = 0;
75+
while (t) {
76+
ans++;
77+
t /= 10;
78+
}
79+
return ans;
80+
}
81+
public:
82+
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
83+
unordered_set<int> can;
84+
for (int t : arr1) {
85+
while (t) {
86+
can.insert(t);
87+
t /= 10;
88+
}
89+
}
90+
91+
int ans = 0;
92+
for (int t : arr2) {
93+
while (t) {
94+
if (can.count(t)) {
95+
ans = max(ans, len(t));
96+
break;
97+
}
98+
t /= 10;
99+
}
100+
}
101+
return ans;
102+
}
103+
};
104+
```
105+
106+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/161294062)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/05/21/LeetCode%203043.%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%89%8D%E7%BC%80%E7%9A%84%E9%95%BF%E5%BA%A6/)哦~
107+
>
108+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* @Author: LetMeFly
33
* @Date: 2026-05-13 12:08:45
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-05-14 22:18:54
5+
* @LastEditTime: 2026-05-21 21:54:06
6+
* @Description: MacOS.ok
67
*/
78
#include <iostream>
89
#include <vector>

tryGoPy/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
caldav

0 commit comments

Comments
 (0)