-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·48 lines (46 loc) · 1.41 KB
/
Solution.java
File metadata and controls
executable file
·48 lines (46 loc) · 1.41 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
package $014;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 9:07 2018/3/15.
*/
public class Solution {
public String longestCommonPrefix(String[] strs){
if (strs == null || strs.length == 0){
return "";
}
String targetStr = strs[0];
for (int i = 1; i < strs.length; i++){
if ("".equals(strs[i])){
return "";
}
while (strs[i].indexOf(targetStr) != 0){
if (targetStr.length() <= 1){
return "";
}
targetStr = targetStr.substring(0, targetStr.length()-1);
}
}
// String targetStr = "";
// for(String str : strs){
// if ("".equals(str)){
// return "";
// }
// if ("".equals(targetStr)){
// targetStr = str;
// continue;
// }
// while (str.indexOf(targetStr) != 0){
// if (targetStr.length() <= 1){
// return "";
// }
// targetStr = targetStr.substring(0, targetStr.length()-1);
// }
// }
return targetStr;
}
public static void main(String[] args) {
Solution solution = new Solution();
String[] arr ={"112", "1122", "1133"};
System.out.println(solution.longestCommonPrefix(arr));
}
}