Longest Common Prefix

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

Show Tags

SOLUTION 1:

解法很直观。先找到最小长度,然后逐个字母遍历,同时逐个遍历所有的字符串。注意各种小细节:

1. break的时候,应该返回上一个索引指向的子串。

2. 如果没有break,表示minlen长度的字串就是最大pre.

 public class Solution {
//http://blog.csdn.net/fightforyourdream/article/details/14642079
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == ) {
// bug 2: should not return null.
return "";
} // Find out the shortest length.
String s0 = strs[];
int len = s0.length();
for (String s: strs) {
len = Math.min(len, s.length());
} // The index of the character which is examing.
// Bug 3: 当不会break的时候,结果是错的
// Bug 4: forget to add int i = 0;
for (int i = ; i < len; i++) {
// Bug 1: forget to write charAt(i);
char c = s0.charAt(i);
for (int j = ; j < strs.length; j++) {
if (strs[j].charAt(i) != c) {
// Bug 5: write substring to sbustring
return s0.substring(, i);
}
}
} // Never break, means strs[0].0-len is the solution.
return s0.substring(, len);
}
}

2015.1.2 redo:

 public class Solution {
public String longestCommonPrefix(String[] strs) {
String ret = "";
if (strs == null || strs.length == 0) {
return ret;
} int minLen = Integer.MAX_VALUE;
for (String str: strs) {
minLen = Math.min(str.length(), minLen);
} for (int i = 0; i < minLen; i++) {
for (int j = 1; j < strs.length; j++) {
if (strs[0].charAt(i) != strs[j].charAt(i)) {
return strs[0].substring(0, i);
}
}
} return strs[0].substring(0, minLen);
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/LongestCommonPrefix_1221_2014.java

LeetCode: Longest Common Prefix 解题报告的更多相关文章

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

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

  2. Leetcode Longest Common Prefix

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

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

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

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

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

  5. LeetCode——Longest Common Prefix

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

  6. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  7. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  8. Lintcode: Longest Common Substring 解题报告

    Longest Common Substring 原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/# Given tw ...

  9. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

随机推荐

  1. rabbitMQ概念详细介绍

    1. 历史 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有 ...

  2. Linux安装ElasticSearch-2.2.0-分词器插件(Mmseg)

    1.在gitpub上搜索elasticsearch-analysis,能够看到所有elasticsearch的分词器: 2.安装Mmseg分词器:https://github.com/medcl/el ...

  3. ORA-65179: cannot keep datafiles for a pluggable database that is not unplugged

    SQL> drop pluggable database pdb2; drop pluggable database pdb2 * ERROR at line : ORA-: cannot ke ...

  4. MySQL执行mysql_install_db初始化

    # ./mysql_install_db \ > --defaults-file=/etc/my.cnf \ > --basedir=/data/mysql \ > --datadi ...

  5. Asp.net(C#)常用正则表达式封装

    using System; using System.Collections; using System.Text.RegularExpressions; namespace MSCL { //// ...

  6. 第一篇:初识ASP.NET控件开发_第二节:HelloWorld

    1)步骤一:新建类库项目:Controls,创建新解决方案:CustomLibrary 2)步骤二:在类库项目中添加“ASP.NET服务器控件”新建项:RenderHelloWorld.cs (也可以 ...

  7. Linux版迅雷(Xware)安装配置方法

    Linux版迅雷下载请见:Xware下载 解压下载的压缩包 核对MD5或SHA1校验值 解压得到以下三个文件 安装文件 在/usr/local/下创建thunder文件夹,将上面的三个文件放入,然后运 ...

  8. C#基础第三天-作业答案-集合-冒泡排序-模拟名片

    .冒泡排序 Console.WriteLine("对集合里的数进行排序,请输入第一个数:"); int a = int.Parse(Console.ReadLine()); Con ...

  9. jQueryWEUI自定义对话框-带有textarea

    jQueryWEUI  示例下载 在jQueryWEUI中提供了很多类型的对话框, 可以去访问看一下. 今天记录的则是,自己定义的一个带有文本域的对话框,这样,可以不通过调转页面,实现一些信息的提交. ...

  10. MySQL 示例数据库 employees 详解

    [引子] IT这一行在我看来是比较要求动手能力的,但是人非生而知之:人们身上的技能除了一些本能之外,大多都是通过学习而得到的. 前一段时间一直在整理素材,写一个关于explain 的系列文章:在一开始 ...