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. java性能调优及问题追踪--Btrace的使用

    在生产环境中经常遇到格式各样的问题,如OOM或者莫名其妙的进程死掉.一般情况下是通过修改程序,添加打印日志:然后重新发布程序来完成.然而,这不仅麻烦,而且带来很多不可控的因素.有没有一种方式,在不修改 ...

  2. [Sass]嵌套

    [Sass]嵌套-选择器嵌套 Sass 中还提供了选择器嵌套功能,但这也并不意味着你在 Sass 中的嵌套是无节制的,因为你嵌套的层级越深,编译出来的 CSS 代码的选择器层级将越深,这往往是大家不愿 ...

  3. APM的飞行模式

    1.稳定模式Stabilize 稳定模式是使用得最多的飞行模式,也是最基本的飞行模式,起飞和降落都应该使用此模式. 此模式下,飞控会让飞行器保持稳定,是初学者进行一般飞行的首选,也是FPV第一视角飞行 ...

  4. 使用javax.servlet.http.Part类上传文件

    使用的是Servlet 3.0 新的特征标注(Annotaion)类描述部署,一些低版本的服务器需要使用标准依赖部署描述文件(web.xml)来部署,另外Part也是Java EE 6.0新增的类,P ...

  5. mysql 创建用户、授权等操作

    一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...

  6. linux常用命令备忘

    scp使用非默认端口 scp -P port username@.....IP:/dir 要拷贝到的地方

  7. MFC 滑动条的重绘

    MFC自带的滑动条的样子是这样的. 比较难看,所以需要重绘下,重绘后的样子是这样的. 代码如下: CustomSliderCtr.h #pragma once // CCustomSliderCtr ...

  8. 预处理指令 #import

    vs class # import会包含这个类的所有信息,包括实体变量和方法, # @class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,先不考虑.

  9. Restful API

    http://www.ruanyifeng.com/blog/2011/09/restful 参考资料:-------以网络为基础的应用软件的架构设计. Restful API的设计与实践 字数218 ...

  10. 海外建VPS并支持VPN

    推荐 DigitalOcean http://www.digitalocean.com/?refcode=7c26aea99ed6