[LeetCode 题解]: Longest Common Prefix
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的更多相关文章
- leetcode 题解 || Longest Common Prefix 问题
problem: Write a function to find the longest common prefix string amongst an array of strings. 寻找 0 ...
- LeetCode题解——Longest Common Prefix
题目: 给定一系列的字符串,找出这些字符串的最长公共前缀. 解法: 暴力法,依次比较每个字符串的每个字符,碰到第一个不同的就返回之前找到的前缀. 代码: class Solution { public ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- Java [leetcode 14] Longest Common Prefix
小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...
随机推荐
- Oracle导出CSV文件
-- 建立存储过程 CREATE OR REPLACE PROCEDURE SQL_TO_CSV ( P_QUERY IN VARCHAR2, -- PLSQL文 P_DIR IN VARCHAR2, ...
- sitemap和sitemapindex
介绍: https://support.google.com/webmasters/answer/75712?hl=zh-Hans 实例: http://www.thepaper.cn/sitemap ...
- IDEA debug
版权声明: 本文转自:https://blog.csdn.net/qq_27093465/article/details/64124330 1,rerun XXX,这个就是直接重新跑某个程序.2,这个 ...
- 将DataTable中的数据导出成Excel
public bool ExportFile(System.Data.DataTable dt){ SaveFileDialog sfd = new SaveFileDialog(); s ...
- opencv在64位4418上的移植
1.mkdir build 2.cmake-gui 操作系统写Linux 去掉 去掉WITH_CUDA 去掉WITH_GTK 去掉WITH_1394 去掉WITH_GSTREAMER 去掉WITH_L ...
- 关于在64位win7下运行Virtualbox安装系统时出错(提示VBoxDD.DLL错误)的解决方
安装没有问题,安装了最新版VirtualBox-4.3.18-96516-Win,一点运行想安装系统时就出错. 这是提示的错误: 运行Virtualbox去安装系统时出错:Failed to open ...
- It's not too late to start!
It's not too late to start! 以此鼓励,希望能坚持下去,一个半路自学PHP的准PHPer!
- js失去焦点触发
onblur="displayRest($(this))"
- JVM致命错误日志(hs_err_pid.log)解读
JVM致命错误日志(hs_err_pid.log)解读 摘自:https://blog.csdn.net/u013938484/article/details/51811400 2016年07月02日 ...
- Excel分类汇总与数据有效性
分类汇总就是把一些数据按照一个标准进行分类,然后按照相应的汇总方式进行汇总. 使用分类汇总之前先排序,否则汇总会出现很多类. 看如上这个表,如果按照所属区域分类,然后按照金额的总和汇总,在汇总之前就要 ...