be careful of the special cases(null, 0)
1 public class Solution { 2 public String longestCommonPrefix(String[] strs) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(strs == null||strs.length == 0) 6 return ""; 7 if(strs.length == 1) 8 return strs[0]; 9 char[] mychar = strs[0].toCharArray(); 10 int max = mychar.length; 11 for(int i = 1; i < strs.length; i++) 12 { 13 char[] tmp = strs[i].toCharArray(); 14 int tmpmax = 0; 15 for(int j = 0; j < Math.min(mychar.length, tmp.length); j++) 16 { 17 if(mychar[j] == tmp[j]) 18 tmpmax++; 19 else 20 break; 21 } 22 max = Math.min(max,tmpmax); 23 } 24 return strs[0].substring(0,max); 25 } 26 }