LintCode-最长公共前缀

给k个字符串,求出他们的最长公共前缀(LCP)

您在真实的面试中是否遇到过这个题? 
Yes
样例

在 "ABCD" "ABEF" 和 "ACEF" 中,  LCP 为 "A"

在 "ABCDEFG", "ABCEFG", "ABCEFA" 中, LCP 为 "ABC"

标签  Expand  


分析:就是直接两两计算前缀。。。

代码:

class Solution {
public:    
    /**
     * @param strs: A list of strings
     * @return: The longest common prefix
     */
    string longestCommonPrefix(vector &strs) {
        // write your code here
        if(strs.size()==0)
            return "";
        string ret = strs[0];
        for(int i=1;i


你可能感兴趣的:(面试)