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

题解: 寻找一组字符串的最长公共前缀。 

最简单的方法,用一个字符串记录当前最长的公共前缀,然后依次比较。时间复杂度: O(N).

 class Solution {
public:
string getPrefix(string a,string b) // 辅助函数用于获取两个字符串的公共前缀
{
string ans;
for(int i=;i<a.size() && i<b.size();i++)
{
if(a[i]==b[i]) ans+=a[i];
else break;
}
return ans;
}
string longestCommonPrefix(vector<string> &strs) {
string a;
int len = strs.size();
if(!len) return a;
a = strs[];
for(int i=;i<len;i++) a= getPrefix(a,strs[i]);
return a;
}
};

高效的方法,采用分治法,先分块,后合并比较,时间复杂度O(logN)。

 class Solution {
public:
string getPrefix(string a,string b) //辅助函数,用于获取两个字符串的公共前缀
{
string ans;
for(int i=;i<a.size() && i<b.size();i++)
{
if(a[i]==b[i]) ans+=a[i];
else break;
}
return ans;
}
string commonPrefix(int left,int right, vector<string> &strs) //分治法寻找commonPrefix
{
if(left>=right) return strs[left];
if(left+==right) return getPrefix(strs[left],strs[right]);
int middle = (right+left)>>;
return getPrefix(commonPrefix(left,middle,strs),commonPrefix(middle+,right,strs));
}
string longestCommonPrefix(vector<string> &strs) {
int i=,len=strs.size();
string a;
if(!len) return a;
return commonPrefix(,len-,strs);
}
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Longest Common Prefix的更多相关文章

  1. leetcode 题解 || Longest Common Prefix 问题

    problem: Write a function to find the longest common prefix string amongst an array of strings. 寻找 0 ...

  2. LeetCode题解——Longest Common Prefix

    题目: 给定一系列的字符串,找出这些字符串的最长公共前缀. 解法: 暴力法,依次比较每个字符串的每个字符,碰到第一个不同的就返回之前找到的前缀. 代码: class Solution { public ...

  3. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  4. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

  5. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  6. [LeetCode] 14. Longest Common Prefix

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

  7. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  8. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  9. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

随机推荐

  1. Java的native关键字以及JNI

    http://blog.csdn.net/yangjiali014/article/details/1633017 这篇博文相当清楚了 包括native关键字的介绍,JNI的书写步骤,以及JNI的实现 ...

  2. fileinput模块可以循环一个或多个文本文件的内容

    fileinput模块可以循环一个或多个文本文件的内容. [默认格式] fileinput.input (files=None, inplace=False, backup='', bufsize=0 ...

  3. 关于BigDecimal类型在jsp页面中进行除法运算问题

    出自:http://blog.csdn.net/u011910290/article/details/52935337 问题描述: 在项目编写过程中,发现BigDecimal在jsp中进行除法运算时, ...

  4. msql修改密码

    修改的用户都以root为列.一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p password " ...

  5. Unable to save settings: Failed to save settings. Please restart IntelliJ IDEA

    找到原先备份的项目,把 .idea 文件夹重新覆盖,解决问题.

  6. Spring 学习记录4 ResourceLoader

    ResourceLoader Spring的ApplicationContext继承了ResourceLoader接口.这个接口主要就是可以加载各种resource.. 接口还是比较简单的: /* * ...

  7. RouterOS DNS劫持(转)

    什么是DNS劫持 DNS劫持就是通过技术手段,来控制用户解析域名的IP地址.举个例子,正常解析域名www.awolf.net时应该返回IP:64.64.30.60:但现在通过DNS劫持,使域名www. ...

  8. 32. Longest Valid Parentheses (Stack; DP)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. CloudStack Ctrix官网版本

    手动生成keystore keytool -genkey -keystore /etc/cloudstack/management/cloud.keystore -storepass "vm ...

  10. Python学习笔记_使用openpyxl操作Excel,在同一个文件里复制某一个sheet

    应用场景:定制一个Excel模板文件,其中定义了一个模板Sheet,以此模板文件里的模板sheet为样例,制作报表,里面有不止一个模板样例Sheet 一.软件环境: 1.OS:Win10 64位 2. ...