leetcode-----14. 最长公共前缀

代码

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) return "";
        String ans = strs[0];
        for (int i = 1; i < strs.length; ++i) {
            while (strs[i].indexOf(ans) != 0) {
                ans = ans.substring(0, ans.length() - 1);
                if (ans.isEmpty()) return "";
            }
        }
        return ans;
    }
}
posted @ 2020-06-13 15:51  景云ⁿ  阅读(71)  评论(0)    收藏  举报