Skip to content

Latest commit

 

History

History
17 lines (16 loc) · 430 Bytes

14. 最长公共前缀.md

File metadata and controls

17 lines (16 loc) · 430 Bytes
class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs[0].length() == 0) return "";
        if (strs.length == 1) return strs[0];
        String str = strs[0];
        for (int i = 0; i < strs.length; i++) {
            if (!strs[i].startsWith(str)) {
                str = str.substring(0, str.length() - 1);
                i--;
            }
        }
        return str;
    }
}