Skip to content

14-最长公共前缀

简单题就不废话了

java
class Solution {
    public String longestCommonPrefix(String[] strs) {
        StringBuffer sb = new StringBuffer();
        int minLength  = strs[0].length();
        for(String s :strs){
            if (s.length() < minLength){
                minLength = s.length();
            }
        }
        for(int i = 0;i<minLength;i++){
            char tmp = strs[0].charAt(i);
            for(int j = 0; j<strs.length ;j++){
                if(strs[j].charAt(i)!= tmp){
                    return sb.toString();
                }
            }
            sb.append(tmp);
        }
        return sb.toString();
    }
}