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. JavaScript Boolean Object 逻辑

    Create a Boolean Object The Boolean object represents two values: "true" or "false&qu ...

  2. 遇到影响服务器性能的cpuspeed 服务

    最近碰到一个很蛋痛的问题,,我在公司的代码上实现了一个功能,然后基于这个测试,结果比对数据发现每天少三千多万条,, 然后我各种优化,各种零碎部功能阉割,,还是丢数据! 之后,监控运行网卡----wat ...

  3. Android——检测TXT文件中是否含有双字节字符

    在读取双字节字符时,主要涉及到编码的选取: public static boolean isRightfulTXT(File f) { // TODO Auto-generated method st ...

  4. int和Integer之间的区别和联系

          在工作中使用==埋下的坑这篇博文中,我们看到当使用基本类型的时候==是完全没有问题的,部分或者混合使用基本类型和装箱基本类型的时候,就可能出现问题了,那么我们可能会想基本类型和装箱基本类型 ...

  5. mysql合并binlog

    例如: PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL DAY);

  6. swagger配置和简单使用

    说明:本地环境idea + maven3.5 + springboot2.0.0 + springfox-swagger2 2.8.0  + springfox-swagger-ui 2.8.0 +  ...

  7. XML5个转义符

    XML5个转义符:<,>,&,”,©;的转义字符分别如下: < >& " &apos;

  8. navicat cannot create oci 解决

    1.新建连接 2.连接时候报错 3.解决 3.1.选择对应版本OCI 3.1.配置OCI[工具->选项...]

  9. CSS加DIV布局

    第一种: <div> <div class="right"> <p></p> <p></p> <p&g ...

  10. Android 启动、绘制、显示过程

    Activity 启动过程: startActivity()-> Instrumentation.execStartActivity()-> Binder->ActivityMana ...