Compare commits
No commits in common. "a83429a6b794491f269fccaa74b710ca4abc514b" and "8ce22bbbbbc52f16668a0b16e998d645e9c681f6" have entirely different histories.
a83429a6b7
...
8ce22bbbbb
|
|
@ -106,55 +106,33 @@ public class Solution {
|
|||
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) {
|
||||
if (strs == null || strs.length == 0) {
|
||||
// ["flower","flow","flight"]
|
||||
int maxPrefix = 0;
|
||||
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);
|
||||
for (int i = 1; i < strs.length; i++) {
|
||||
if (strs[i] != ""){
|
||||
for (int j = 0; j < 1; j++) {
|
||||
char crrChar = strs[i].charAt(index);
|
||||
if (indexChar != crrChar){
|
||||
return String.valueOf(temp);
|
||||
}
|
||||
if (i == strs.length- 1){
|
||||
temp.append(crrChar);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
int length = strs[0].length();
|
||||
int count = strs.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
char c = strs[0].charAt(i);
|
||||
for (int j = 1; j < count; j++) {
|
||||
if (i == strs[j].length() || strs[j].charAt(i) != c) {
|
||||
return strs[0].substring(0, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return strs[0];
|
||||
}
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ public class test {
|
|||
// int n = 0;
|
||||
// Solution.merge(nums1,m,nums2,n);
|
||||
// String[] strs = new String[]{"flower","flow","flight"};
|
||||
String[] strs = new String[]{"flower","flower","flower","flower"};
|
||||
String[] strs = new String[]{"abcg","abcecar","abr"};
|
||||
|
||||
String result = Solution.longestCommonPrefix(strs);
|
||||
System.out.println(result);
|
||||
|
|
|
|||
Loading…
Reference in New Issue