public class Solution {
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
public String longestCommonPrefix(String[] strs) {
// write your code here
if(strs.length == 0){
return "";
}
String prefix = strs[0];
int i = 0;
int j = 0;
for ( i=1;i<strs.length;i++){
if(strs[i].length() == 0){
return "";
}
for( j=0;j<prefix.length();j++){
if(strs[i].charAt(j) != prefix.charAt(j)){
prefix = prefix.substring(0,j);
}
}
}
return prefix;
}
}

 思路:

  默认数组第一个是prefix,然后循环比较数组中的每一个字符串,直到不相等,返回substring即可,返回的substring一定是最长的公共前缀。

  注:判断数组为空或者数组中有空字符串

时间复杂度:O(第一个字符串长度^2),java耗时1563 ms。

LintCode 78:Longest Common Prefix的更多相关文章

  1. 78. Longest Common Prefix【medium】

    Given k strings, find the longest common prefix (LCP).   Example For strings "ABCD", " ...

  2. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

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

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

  4. 【leetcode】Longest Common Prefix

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

  5. 14. Longest Common Prefix

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

  6. Leetcode Longest Common Prefix

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

  7. [LeetCode] 14. Longest Common Prefix

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

  8. No.014:Longest Common Prefix

    问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...

  9. 68. Longest Common Prefix

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

随机推荐

  1. PHP 文件夹操作「复制、删除、查看大小」迭代实现

    "既然递归能很好的解决,为什么还要用迭代呢"?主要的原因还是效率问题-- 递归的概念是函数调用自身,把一个复杂的问题分解成与其相似的多个子问题来解决,可以极大的减少代码量,使得程序 ...

  2. CAIN怎么嗅探路由密码

    Cain & Abel 是由Oxid.it开发的一个针对Microsoft操作系统的免费口令恢复工具.号称穷人使用的L0phtcrack.它的功能十分强大,可以网络嗅探,网络欺骗,破解加密口令 ...

  3. Apache 服务器搭建 总结

    安装素材准备:<1>下载jdk <2>下载apache2.0.55 <3>下载tomcat5.5 <4>下载jk(mod_jk-apache-2.0.5 ...

  4. Transaction事务传播行为种类PROPAGATION_REQUIRED

    事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 表1事务传播行为类型 事务传 ...

  5. WebServices复习

  6. mybatis如何做分页处理

    1.首先根据自己实际需求编写实体类 import java.io.Serializable; public class User implements Serializable{ //最好将该实体类序 ...

  7. iOS开发CoreGraphics核心图形框架之一——CGPath的应用

    一.引言    CoreGraphics核心图形框架相较于UIKit框架更加偏于底层.在Objective-C工程中,CoreGraphics其中方法都是采用C语言风格进行编写的,同时其并不支持Obj ...

  8. 模拟搭建Web项目的真实运行环境(七)

    下面这个是mongo驱动的小案例,里面也有涉及到一点redis的操作 https://github.com/SuperRocky/MyMongoDriver 接下来通过几张图片主要介绍一下每个文件的具 ...

  9. useradd与adduser的区别

    useradd与adduser都是创建新的用户 在CentOs下useradd与adduser是没有区别的都是在创建用户,在home下自动创建目录,没有设置密码,需要使用passwd命令修改密码. 而 ...

  10. Oracle Database 12c Release 1下载安装(自身经历)

    1.访问Oracle官网:https://www.oracle.com/index.html,下载Oracle Database 12c Release 1 (注意:File1和File2都要下载!! ...