14. Longest Common Prefix

题目:

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

链接:http://leetcode.com/problems/longest-common-prefix/

题解:

求最长前缀。按列计算,判断条件是当前列字符是否相同,以及当前行的长度是否有效。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0)
            return "";
        
        for(int j = 0; j < strs[0].length(); j ++)                  // calc in each row
            for(int i = 1; i < strs.length; i ++)                   // calc in each column
                if(j == strs[i].length() || strs[0].charAt(j) != strs[i].charAt(j))
                    return  strs[0].substring(0, j);
                    
        return strs[0];
    }
}

 

二刷:

Java:

Time Complexity - O(mn),  Space Complexity - O(1)

以第一行为基准行,按列比较字符,假如遇到长度更小,或者字符不一样,返回基本行的substring(0, j)

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        for (int j = 0; j < strs[0].length(); j++) {
            for (int i = 1; i < strs.length; i++) {
                if (j >= strs[i].length() || strs[i].charAt(j) != strs[0].charAt(j)) {
                    return strs[0].substring(0, j);
                }
            }
        }
        
        return strs[0];
    }
}

也有利用java String.indexOf的方法,虽然时间复杂度一样,但实际运行起来很快。

Time Complexity - O(mn), Space Complexity - O(n)。

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        String prev = strs[0];
        for (int i = 1; i < strs.length; i++) {
            while (strs[i].indexOf(prev) != 0) {
                prev = prev.substring(0, prev.length() - 1);
            }
        }
        
        return prev;
    }
}

 

Python:

应该是一个很好的练习zip()的地方。下面代码主要来自zcjsword。 先用'*' uppack strs里的strs里的strings,使用zip截取统一的长度,再按行利用unpacked list建立set,假如set中的元素大于1,说明在过程中出现了不match的情况,我们可以返回结果。

class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if not strs:
            return ""
        sz, res = zip(*strs), ""
        for c in sz:
            if len(set(c)) > 1:
                break
            res += c[0]
        return res    

 

三刷:

Java:

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) return "";
        
        for (int j = 0; j < strs[0].length(); j++) {
            for (int i = 1; i < strs.length; i++) {
                if (j == strs[i].length() || strs[i].charAt(j) != strs[0].charAt(j)) return strs[0].substring(0, j);
            }
        }
        return strs[0];
    }
}

 

 

Reference:

https://leetcode.com/discuss/20993/java-code-with-13-lines

https://leetcode.com/discuss/63950/5-line-python-with-zip-and-len-set

posted @ 2015-04-16 09:57  YRB  阅读(411)  评论(0编辑  收藏  举报