- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution14.java
More file actions
Latest commit
executable file
·53 lines (43 loc) · 1.23 KB
/
Copy pathSolution14.java
File metadata and controls
executable file
·53 lines (43 loc) · 1.23 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
41
42
43
44
45
46
47
48
49
50
51
52
53
importjava.util.ArrayList;
importjava.util.List;
/**
* Created by slade on 2019/5/31.
*/
publicclassSolution14 {
publicStringlongestCommonPrefix(String[] strs) {
// 待存储
StringBufferans = newStringBuffer("");
intcnt = 0;
// 快速入口
if (strs.length <= 0 || strs == null) {
return"";
}
if (strs.length == 1) {
returnstrs[0];
}
intminLength = strs[0].length();
for (Stringword : strs) {
if (word.length() < minLength) {
minLength = word.length();
}
}
booleantag = true;
for (inti = 0; i < minLength; i++) {//词长遍历
for (intj = 1; j < strs.length; j++) {//词遍历
if (strs[0].charAt(i) != strs[j].charAt(i)) {
tag = false;
break;
}
}
if (tag) {
ans.append(strs[0].charAt(i));
}
}
returnans.toString();
}
publicstaticvoidmain(String[] args) {
Solution14s = newSolution14();
String[] strs = {"flight"};
System.out.println(s.longestCommonPrefix(strs));
}
}