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. 一行命令解决 xcode升级新版本插件失效问题

    sudo find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth ...

  2. [转] 实现winfrom进度条及进度信息提示,winfrom程序假死处理

    china_xuhua 原文地址 1.方法一:使用线程 功能描述:在用c#做WinFrom开发的过程中.我们经常需要用到进度条(ProgressBar)用于显示进度信息.这时候我们可能就需要用到多线 ...

  3. ffmpeg源码分析二:main函数和transcode函数 (转2)

    原帖地址:http://blog.csdn.net/austinblog/article/details/24804455 首先从main函数看起,关键解释部分已加注释,该函数在ffmpeg.c文件中 ...

  4. springboot整合最新版dubbo以及dubbo-admin的安装

    一.安装前准备 由于dubbo被阿里捐献给了apache,这次安装admin时,参考网上的资料,地址还是停留在之前的链接,踩了不少坑,这里记录下. dubbo-admin下载地址: 地址一:https ...

  5. 什么时候必须使用UI相机? 多个相机的作用原理?

    首先,要从主画布说起,maincanvas,这个有什么限制?主画布是一张默认用来绘制UI的地方,这些UI必须是系统提供的UI组件,在画面下挂一个3D物体或非UI的2D物品是不会被绘制到画布上的,但是仍 ...

  6. 【转】内存耗用:VSS/RSS/PSS/USS

    Terms VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存) PSS- Prop ...

  7. UNITY引擎变量调用产生不必要内存分配

    https://unity3d.com/de/learn/tutorials/topics/performance-optimization/optimizing-garbage-collection ...

  8. 出现The folder is already a source folder

    右键build path -> configure build path -> source ,选择 src/main/java.src/test/java删除,然后再新建.

  9. java.lang.NoSuchMethodError: org.springframework.dao.IncorrectResultSizeDataAccessException

    spring data jpa  运用,在dao类中写自己新增的方法,使用@query写hql语句,出现以下异常: Caused by: java.lang.NoSuchMethodError: or ...

  10. Invoke and BeginInvoke(转载:http://www.cnblogs.com/worldreason/archive/2008/06/09/1216127.html)

    一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是dotnet程序员众所周知的,我在此费点笔墨再次记录到自己的日志,以便日后提醒一下自己. 1 ...