Skip to content

Commit d5281d5

Browse files
committed
二刷14
1 parent df03096 commit d5281d5

File tree

7 files changed

+81
-19
lines changed

7 files changed

+81
-19
lines changed

docs/0014-longest-common-prefix.adoc

+54-19
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,72 @@
11
[#0014-longest-common-prefix]
2-
= 14. Longest Common Prefix
2+
= 14. 最长公共前缀
33

4-
{leetcode}/problems/longest-common-prefix/[LeetCode - Longest Common Prefix^]
4+
https://leetcode.cn/problems/longest-common-prefix/[LeetCode - 14. 最长公共前缀 ^]
55

6-
Write a function to find the longest common prefix string amongst an array of strings.
6+
编写一个函数来查找字符串数组中的最长公共前缀。
77

8-
If there is no common prefix, return an empty string `""`.
8+
如果不存在公共前缀,返回空字符串 `""`
99

10-
*Example 1:*
10+
*示例 1:*
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* ["flower","flow","flight"]
15-
*Output:* "fl"
16-
----
12+
....
13+
输入:strs = ["flower","flow","flight"]
14+
输出:"fl"
15+
....
1716

18-
*Example 2:*
17+
*示例 2:*
1918

20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* ["dog","racecar","car"]
23-
*Output:* ""
24-
*Explanation:* There is no common prefix among the input strings.
25-
----
19+
....
20+
输入:strs = ["dog","racecar","car"]
21+
输出:""
22+
解释:输入不存在公共前缀。
23+
....
24+
25+
26+
*提示:*
27+
28+
* `+1 <= strs.length <= 200+`
29+
* `+0 <= strs[i].length <= 200+`
30+
* `strs[i]` 仅由小写英文字母组成
31+
32+
33+
== 思路分析
34+
35+
最简单的解题办法是逐列扫描。看题解,也不需要保存前缀,发现不同时,直接从字符串中截取即可。
2636

27-
*Note:*
37+
image::images/0014-10.png[{image_attr}]
38+
39+
image::images/0014-11.png[{image_attr}]
40+
41+
image::images/0014-12.png[{image_attr}]
42+
43+
image::images/0014-13.png[{image_attr}]
2844

29-
All given inputs are in lowercase letters `a-z`.
3045

3146

3247
[[src-0014]]
48+
[tabs]
49+
====
50+
一刷::
51+
+
52+
--
3353
[{java_src_attr}]
3454
----
3555
include::{sourcedir}/_0014_LongestCommonPrefix.java[tag=answer]
3656
----
57+
--
58+
59+
二刷::
60+
+
61+
--
62+
[{java_src_attr}]
63+
----
64+
include::{sourcedir}/_0014_LongestCommonPrefix_2.java[tag=answer]
65+
----
66+
--
67+
====
68+
69+
70+
== 参考资料
3771

72+
. https://leetcode.cn/problems/longest-common-prefix/solutions/288575/zui-chang-gong-gong-qian-zhui-by-leetcode-solution/[14. 最长公共前缀 - 官方题解^] -- 解法很多!

docs/images/0014-10.png

131 KB
Loading

docs/images/0014-11.png

195 KB
Loading

docs/images/0014-12.png

138 KB
Loading

docs/images/0014-13.png

184 KB
Loading

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,11 @@ endif::[]
698698
|{doc_base_url}/0994-rotting-oranges.adoc[题解]
699699
|✅ 腐烂加剧+传染。广度优先遍历的解法也挺有意思。
700700

701+
|{counter:codes2503}
702+
|{leetcode_base_url}/longest-common-prefix/[14. Longest Common Prefix^]
703+
|{doc_base_url}/0014-longest-common-prefix.adoc[题解]
704+
|✅ 解法很多,可以很好地开阔思路!
705+
701706
|===
702707

703708
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0014_LongestCommonPrefix_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-16 15:58:55
9+
*/
10+
public String longestCommonPrefix(String[] strs) {
11+
for (int i = 0; i < strs[0].length(); i++) {
12+
char c = strs[0].charAt(i);
13+
for (int j = 1; j < strs.length; j++) {
14+
if (i >= strs[j].length() || c != strs[j].charAt(i)) {
15+
return strs[0].substring(0, i);
16+
}
17+
}
18+
}
19+
return strs[0];
20+
}
21+
// end::answer[]
22+
}

0 commit comments

Comments
 (0)