[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 ...
随机推荐
- freebsd静态路由
FreeBSD下增进静态路由的行动 1.手工添加 # route add -net 192.168.2.0/24 192.168.1.2 2. 通过rc.conf永世 设置 # Add static ...
- stm32库函数FSMC_NORSRAMInit()解析
这是一段对nor存储器的时序进行编程的函数,函数形式为void FSMC_NORSRAMInit(FSMC_NORSRAMInitTypeDef* FSMC_NORSRAMInitStruct),里面 ...
- 【读书笔记】 DevOps实践 - 驭DevOps之力强化技术栈并优化IT运行
读书小结 DevOps实践 - 驭DevOps之力强化技术栈并优化IT运行 这本书共200页,读完大概三天:(我指的不是fulltime的一天,而是工作时间以外的一天) 本书是参加16年QConf开发 ...
- aop中通知详情
- java核心知识点 --- 线程池ThreadPool
线程池是多线程学习中需要重点掌握的. 系统启动一个新线程的成本是比较高的,因为它涉及与操作系统交互.在这种情形下,使用线程池可以很好的提高性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更应该考 ...
- AttributeUsage
[AttributeUsage] System.AttributeUsage声明一个Attribute的使用范围与使用原则. AllowMultiple 和 Inherited 参数是可选的,所以此代 ...
- Professional C# 6 and .NET Core 1.0 - Chapter 42 ASP.NET Web API
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处: -------------------------------------------------------- ...
- Jenkins中Jelly邮件模板的配置
[链接]Jenkins中Jelly邮件模板的配置http://blog.csdn.net/hwhua1986/article/details/47975237
- 273. Integer to English Words数字转为单词
[抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
- 79. Word Search在字母矩阵中查找单词
[抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...