查找字符串数组中的最长公共前缀。
纵向扫描:
public String longestCommonPrefix(String[] strs) { if (strs == null || strs.length==0) return ""; String result = ""; int minLength = strs[0].length(); for (String st : strs ){ if (st.length()<minLength) { minLength = st.length(); } } for (int i = 0; i < minLength; i++) { char c = strs[0].charAt(i); for (String s :strs) { if (s.charAt(i) != c ){ return result; } } result += c; } return result; }