题记:

这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描。

横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串。

纵向扫描:对所有串,从字符串第0位开始比较,全部相等则继续比较第1,2...n位,直到发生不全部相等的情况,则得出最长公共前缀串。

横向扫描算法实现:

//LeetCode_Longest Common Prefix
//Written by zhou
//2013.11.22 class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case. if (strs.size() == )
return ""; string prefix = strs[];
for (int i = ; i < strs.size(); ++i)
{
if (prefix.length() == || strs[i].length() == )
return ""; int len = prefix.length() < strs[i].length() ? prefix.length() : strs[i].length(); int j; for (j = ; j < len; ++j)
{
if (prefix[j] != strs[i][j])
break;
} prefix = prefix.substr(,j); } return prefix;
}
};

纵向扫描代码实现:

function longestCommonPrefix(strs:Array):String{
if(strs == null || strs.length == 0)return; for(i:int = 0; i < str[0].length - 1; i++)
{
for(j:int = 0; j < str.length - 1; j++)
{
if(str[i].charAt(j) != str[0].charAt(j))return strs[0].substr(0,j);
} }
return strs[0];
}

[转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀的更多相关文章

  1. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  2. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  3. [LeetCode] Longest Common Prefix 字符串公有前序

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

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

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  5. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

  6. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  7. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  8. Leetcode Longest Common Prefix

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

  9. leetcode.字符串.14最长公共前缀-Java

    1. 具体题目 编写一个函数来查找字符串数组中的最长公共前缀.如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","fl ...

随机推荐

  1. 深入理解ASP.NET 5的依赖注入

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:ASP.NET 5整个底层都架构于依赖注入机制之下,今天介绍的文章详细介绍了内置依赖注 ...

  2. Ubuntu14.04LTS系统输入法的安装

    由于安装的时候选择的是英文版,所以一进入系统问题就来了:无法输入中文. 我记得自己直接选的输入法是pinyin那个 在网上看到别人到blog,直接转过来吧,只为自己收藏下,如有需要请联系原作者. 转载 ...

  3. win10 Edge浏览器一打开就闪退崩溃的解决思路

    故障现象:从Win7.Win8.1升级到Win10,或是使用Win10一段时间后,发现Edge浏览器打开一到两秒就闪退,崩溃无法打开.解决方案: 1.尝试清理: C:\Users\Administra ...

  4. ListView中每个item条目在被单击选中时能够高亮显示

    在布局文件中设定: android:listSelector="@android:color/holo_red_light" 在代码中实现 listView.setSelector ...

  5. 处理滚动条置底的JS代码

    function scrollTopToBottom(){ var seffheight =$(".consultingRecords_view").height(); $(&qu ...

  6. Android简单自定义圆形和水平ProgressBar

    ProgressBar简介 继承于View类,直接子类有AbsSeekBar和ContentLoadingProgressBar,其中AbsSeekBar的子类有SeekBar和RatingBar,可 ...

  7. MySQL导入sql脚本 导出数据库

    导出数据库 不能停止服务 cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) mysqldump -u用户名 -p 数据库名 > 导出的文件名 ...

  8. HTML5 重要标签及其属性学习

    1.google字体:<link href="https://fonts.googleapis.com/css?family=Lobster" rel="style ...

  9. Visual Studio 设置 Inherited include Directories

    在用Visual Studio进行开发的时候,避免不了要使用一些常用的第三方提供的库.如果是一次两次设置还能让人忍受,但是如果要写很多项目的话,设置这些库真的很让人头疼.不过Visual Studio ...

  10. java实现文件上传--flash上传

    1.http请求的头信息是“application/octet-stream”,request body 是二进制的flash图片流 2.把流中的信息读入到文件中 代码如下,代码分三个部分: ---- ...