Compare commits
2 Commits
8ce22bbbbb
...
a83429a6b7
| Author | SHA1 | Date |
|---|---|---|
|
|
a83429a6b7 | |
|
|
654ae87668 |
|
|
@ -106,33 +106,55 @@ public class Solution {
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public static String longestCommonPrefix(String[] strs) {
|
||||||
|
// int index = 0;
|
||||||
|
// StringBuilder temp = new StringBuilder();
|
||||||
|
// if (strs != null){
|
||||||
|
// for (int k = 0; k < strs[0].length(); k++) {
|
||||||
|
// char indexChar = strs[0].charAt(index);
|
||||||
|
// if (strs.length == 1){
|
||||||
|
// return strs[0];
|
||||||
|
// }
|
||||||
|
// for (int i = 1; i < strs.length; i++) {
|
||||||
|
// if (strs[i] != ""){
|
||||||
|
// for (int j = 0; j < 1; j++) {
|
||||||
|
// if (index >= strs[i].length()){
|
||||||
|
// return String.valueOf(temp);
|
||||||
|
// }
|
||||||
|
// char crrChar = strs[i].charAt(index);
|
||||||
|
// if (indexChar != crrChar){
|
||||||
|
// return String.valueOf(temp);
|
||||||
|
// }
|
||||||
|
// if (i == strs.length- 1){
|
||||||
|
// temp.append(crrChar);
|
||||||
|
// index++;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// return "";
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return String.valueOf(temp);
|
||||||
|
// }
|
||||||
|
// return "";
|
||||||
|
// }
|
||||||
|
|
||||||
public static String longestCommonPrefix(String[] strs) {
|
public static String longestCommonPrefix(String[] strs) {
|
||||||
// ["flower","flow","flight"]
|
if (strs == null || strs.length == 0) {
|
||||||
int maxPrefix = 0;
|
return "";
|
||||||
int index = 0;
|
}
|
||||||
StringBuilder temp = new StringBuilder();
|
int length = strs[0].length();
|
||||||
if (strs != null){
|
int count = strs.length;
|
||||||
for (int k = 0; k < strs[0].length(); k++) {
|
for (int i = 0; i < length; i++) {
|
||||||
char indexChar = strs[0].charAt(index);
|
char c = strs[0].charAt(i);
|
||||||
for (int i = 1; i < strs.length; i++) {
|
for (int j = 1; j < count; j++) {
|
||||||
if (strs[i] != ""){
|
if (i == strs[j].length() || strs[j].charAt(i) != c) {
|
||||||
for (int j = 0; j < 1; j++) {
|
return strs[0].substring(0, i);
|
||||||
char crrChar = strs[i].charAt(index);
|
|
||||||
if (indexChar != crrChar){
|
|
||||||
return String.valueOf(temp);
|
|
||||||
}
|
|
||||||
if (i == strs.length- 1){
|
|
||||||
temp.append(crrChar);
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return "";
|
return strs[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ public class test {
|
||||||
// int n = 0;
|
// int n = 0;
|
||||||
// Solution.merge(nums1,m,nums2,n);
|
// Solution.merge(nums1,m,nums2,n);
|
||||||
// String[] strs = new String[]{"flower","flow","flight"};
|
// String[] strs = new String[]{"flower","flow","flight"};
|
||||||
String[] strs = new String[]{"abcg","abcecar","abr"};
|
String[] strs = new String[]{"flower","flower","flower","flower"};
|
||||||
|
|
||||||
String result = Solution.longestCommonPrefix(strs);
|
String result = Solution.longestCommonPrefix(strs);
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue