1 public class Solution { 2 public String get(String a,String b) 3 { 4 5 if(a==""||b=="") return ""; 6 int len1=a.length(); 7 int len2=b.length(); 8 int len=len1; 9 if(len>=len2) len=len2; 10 String s=""; 11 for(int i=0;i<len;i++) 12 { 13 if(a.charAt(i)==b.charAt(i)) 14 { 15 s+=a.charAt(i); 16 } 17 else break; 18 19 } 20 return s; 21 } 22 23 24 public String longestCommonPrefix(String[] strs) { 25 int len=strs.length; 26 if(len==0) return ""; 27 if(len==1) return strs[0]; 28 String ans=get(strs[0],strs[1]); 29 if(ans=="") return ""; 30 else 31 { 32 for(String s:strs) 33 { 34 ans=get(s,ans); 35 if(ans=="") return ""; 36 } 37 38 39 40 41 42 } 43 return ans; 44 45 46 47 48 } 49 }
leetcode Longest Common Prefix 多个字符串的最长字串,布布扣,bubuko.com
leetcode Longest Common Prefix 多个字符串的最长字串
原文:http://www.cnblogs.com/hansongjiang/p/3840660.html