[LeetCode]Longest Common Prefix

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        int len = 0;
        while (true) {
            if (strs[0].length() < len) {
                return strs[0].substring(0, len - 1);
            }
            String prefix = strs[0].substring(0, len);
            for (String str : strs) {
                if (str.length() < prefix.length() || !str.startsWith(prefix)) {
                    return str.substring(0, len - 1);
                }
            }
            len ++;
        }
    }
}

 

posted @ 2015-12-01 15:55  Weizheng_Love_Coding  阅读(119)  评论(0编辑  收藏  举报