https://leetcode.com/problems/longest-common-prefix/discuss/
輸入: 字符串數(shù)組
輸出: 求公共最長前綴字符串
思路:
先取出第0個 string, 依次比較后面的字符串,求公共前綴
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length ==0 ){
return "";
}
StringBuilder sb=new StringBuilder();
for(int i=0;i<strs[0].length();i++){
char c=strs[0].charAt(i);
boolean isbreak = false;
for(int j=1;j<strs.length;j++){
if(strs[j].length()<=i||strs[j].charAt(i)!=c){
isbreak=true;
break;
}
}
if(isbreak){
break;
}else{
sb.append(c);
}
}
return sb.toString();
}
}