美文网首页
[Day13]14. Longest Common Prefix

[Day13]14. Longest Common Prefix

作者: Shira0905 | 来源:发表于2017-02-12 00:26 被阅读0次

A EASY problem, just for accomplish today's task.
Already late again

DESCRIPTION:
Write a function to find the longest common prefix string amongst an array of strings.

ANALYSIS:
So easy!
Just was a little worried about TimeLimit. And some problems with index dealing.

SOLUTION:

    public static String longestCommonPrefix(String[] strs) {
        if(strs.length==0)
            return "";
        int l = strs.length;
        int count = 0;
        while (count < strs[0].length()) {
            int i;
            for (i = 0; i < l; i++) {
                if (strs[i].length() == count
                        || strs[i].charAt(count) != strs[0].charAt(count)) {
                    return strs[0].substring(0, count);
                }
            }
            if (i == l)
                count++;
        }
        return strs[0].substring(0, count);
    }

相关文章

网友评论

      本文标题:[Day13]14. Longest Common Prefix

      本文链接:https://www.haomeiwen.com/subject/qpaoittx.html