package LeetCode;
public class TestNum14 {
public static void main(String[] args) {
String[] strs= {"flower","flow","flight"};
TestNum14 str = new TestNum14();
System.out.println(str.longestCommonPrefix(strs));
}
public static String longestCommonPrefix(String[] strs) {
if (strs==null||strs.length==0){
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 = 0; j < count; j++) {
if (i==strs[j].length()||strs[j].charAt(i)!=c){
return strs[0].substring(0,i);
}
}
}
return strs[0];
}
}
LeetCode(力扣)算法14号问题
最新推荐文章于 2022-11-25 14:39:29 发布