import java.util.*; public class Solution { /** * * @param strs string字符串一维数组 * @return string字符串 */ public String longestCommonPrefix (String[] strs) { if(strs == null || strs.length == 0) return "" ; String maxPrefix = strs[0] ; for(int i = 1 ; i < strs.length ; i++) { maxPrefix = samePrefix(maxPrefix , strs[i]) ; } return maxPrefix ; } //求取公共前缀 public String samePrefix(String s1 , String s2) { int i = 0 ; int j = 0 ; while (i < s1.length() && j < s2.length()) { if(s1.charAt(i) != s2.charAt(j)) break ; i ++ ; j ++ ; } return s1.substring(0 , i) ; } }