-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.java
More file actions
33 lines (30 loc) · 827 Bytes
/
Copy path14.java
File metadata and controls
33 lines (30 loc) · 827 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
/**
* Created by peter on 2017/1/22.
*/
public class Solution {
public String longestCommonPrefix(String[] strs) {
String result = "";
if (strs == null || strs.length == 0) {
return "";
}
String first = strs[0];
if (first == null || "".equals(first)) {
return "";
}
for (int i = 1;i <= first.length();i++) {
String temp = first.substring(0, i);
for (int j = 0;j < strs.length;j++) {
if (strs[j] != null && strs[j].startsWith(temp)) {
continue;
} else {
temp = null;
break;
}
}
if (temp != null) {
result = temp;
}
}
return result;
}
}