-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonPrefix.java
More file actions
34 lines (33 loc) · 956 Bytes
/
Copy pathLongestCommonPrefix.java
File metadata and controls
34 lines (33 loc) · 956 Bytes
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
package leetcode.string;
/*
* @description: 14.最长公共前缀
* @param: 字符串数组
* @return: 返回最长公共前缀
* @author: cp
* @date: 2020/8/30
*/
public class LongestCommonPrefix {
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) {
return "";
}
if (strs.length == 1) {
return strs[0];
}
int index = 0;
int minLength = Integer.MAX_VALUE;
//找出字符串最短长度
for (int i = 0; i < strs.length; i++) {
minLength = Math.min(minLength, strs[i].length());
}
for (int i = 0; i < minLength; i++) {
char c = strs[0].charAt(i);
for (int j = 1; j < strs.length; j++) {
if (strs[j].charAt(i) != c)
return strs[0].substring(0, index);
}
index++;
}
return strs[0].substring(0, index);
}
}