-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonPrefix.java
More file actions
40 lines (40 loc) · 1.33 KB
/
LongestCommonPrefix.java
File metadata and controls
40 lines (40 loc) · 1.33 KB
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
package com.cier.solution.array;
// https://leetcode-cn.com/problems/longest-common-prefix/
public class LongestCommonPrefix {
// 水平扫描
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) {
return "";
}
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()){
return "";
}
}
}
return prefix;
}
// 垂直扫描
public String longestCommonPrefix2(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
for (int i = 0; i < strs[0].length(); i++) {
char c = strs[0].charAt(i);
for (int j = 1; j < strs.length; j++) {
if (i == strs[j].length() || strs[j].charAt(i) != c) {
return strs[0].substring(0,i);
}
}
}
return strs[0];
}
public static void main(String[] args) {
LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
longestCommonPrefix.longestCommonPrefix2(new String[]{"flower","flow","flight"});
System.out.println("end");
}
}