leetcode最长公共前缀

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        //!通过ascii码进行比较,只比较两个字符串!!!!!!!!
        int index = 0;
        Arrays.sort(strs);
        for (int i = 0; i < Math.min(strs[0].length(), strs[strs.length - 1].length()); i++) {
            if (strs[0].charAt(i) != strs[strs.length - 1].charAt(i)) {
                break;
            }
            index++;
        }
        return strs[0].substring(0, index);
    }
}

你可能感兴趣的:(leetcode最长公共前缀)