FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/src/main/java/L0014_LongestCommonPrefix.java at master · LjyYano/LeetCode · GitHub
LjyYano
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
125
Star
342
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0014_LongestCommonPrefix.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (58 loc) · 2.46 KB
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0014_LongestCommonPrefix.java
Copy path
File metadata and controls
68 lines (58 loc) · 2.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* 题目链接:https://leetcode.cn/problems/longest-common-prefix/
*
* 编写一个函数来查找字符串数组中的最长公共前缀。
* 如果不存在公共前缀,返回空字符串 ""。
*
* 示例 1:
* 输入:strs = ["flower","flow","flight"]
* 输出:"fl"
*
* 示例 2:
* 输入:strs = ["dog","racecar","car"]
* 输出:""
* 解释:输入不存在公共前缀。
*/
public
class
L0014_LongestCommonPrefix
{
public
String
longestCommonPrefix
(
String
[]
strs
) {
// 如果数组为空,返回空字符串
if
(
strs
==
null
||
strs
.
length
==
0
) {
return
""
;
}
// 如果数组只有一个字符串,返回该字符串
if
(
strs
.
length
==
1
) {
return
strs
[
0
];
}
// 以第一个字符串为基准
String
first
=
strs
[
0
];
// 遍历第一个字符串的每个字符
for
(
int
i
=
0
;
i
<
first
.
length
();
i
++) {
char
c
=
first
.
charAt
(
i
);
// 与其他字符串比较当前位置的字符
for
(
int
j
=
1
;
j
<
strs
.
length
;
j
++) {
// 如果其他字符串已经到达末尾,或者字符不匹配
if
(
i
>=
strs
[
j
].
length
() ||
strs
[
j
].
charAt
(
i
) !=
c
) {
// 返回当前已匹配的子串
return
first
.
substring
(
0
,
i
);
}
}
}
// 如果循环结束还没有返回,说明第一个字符串就是最长公共前缀
return
first
;
}
public
static
void
main
(
String
[]
args
) {
L0014_LongestCommonPrefix
solution
=
new
L0014_LongestCommonPrefix
();
// 测试用例 1
String
[]
strs1
= {
"flower"
,
"flow"
,
"flight"
};
System
.
out
.
println
(
"测试用例 1 结果:"
+
solution
.
longestCommonPrefix
(
strs1
));
// 预期输出:fl
// 测试用例 2
String
[]
strs2
= {
"dog"
,
"racecar"
,
"car"
};
System
.
out
.
println
(
"测试用例 2 结果:"
+
solution
.
longestCommonPrefix
(
strs2
));
// 预期输出:""
// 测试用例 3:空数组
String
[]
strs3
= {};
System
.
out
.
println
(
"测试用例 3 结果:"
+
solution
.
longestCommonPrefix
(
strs3
));
// 预期输出:""
// 测试用例 4:只有一个字符串
String
[]
strs4
= {
"alone"
};
System
.
out
.
println
(
"测试用例 4 结果:"
+
solution
.
longestCommonPrefix
(
strs4
));
// 预期输出:alone
}
}
Back
|
FazBrowse Home
|
New Git URL