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. ios实例开发精品文章推荐(8.14)

    1.iOS源码:俄罗斯方块实现简单的俄罗斯方块游戏.<ignore_js_op> 下载地址:http://www.apkbus.com/android-124628-1-1.html 2. ...

  2. fwrite()的返回值随着格式的不同返回值也不同;

    常用地函数fwrite fwrite()的返回值随着格式的不同返回值也不同: 也是最近涉及到代码才注意到的,汗!!! 转载了一篇文章来说明这个问题:文章地址:http://blog.csdn.net/ ...

  3. session会话保持原理

    1. 什么是会话保持? 会话保持是负载均衡最常见的问题之一,也是一个相对比较复杂的问题.会话保持有时候又叫做粘滞会话(Sticky Sessions).会话保持是指在负载均衡器上的一种机制,可以识别客 ...

  4. zabbix数据库需要多大硬盘?我告诉你

    本次案例:100台服务器,每台服务器有30个监控项,每个监控项60秒刷新一次,需要多大的硬盘呢?众所周知,zabbix基本都是通过web配置,这些配置数据也是存放到数据库里的,但是它对硬盘容量的要求基 ...

  5. hibernate 注解 boolean问题解决方案

    1.JPA本身是不支持boolean.可以用Hibernater自带的标签.修改如下. @Column(name = "manager_log") @org.hibernate.a ...

  6. Xcode 各版本简介

    1.Xcode 验证 在终端输入 spctl 命令,并带上安装的 Xcode 的路径 $ spctl --assess --verbose /Applications/Xcode.app 之后会看到类 ...

  7. Oracle - 层次查询

    如果表中含有层次数据,可以通过使用层次查询有序地查看层次数据. 语法: condition:指一个或多个表达式和逻辑(布尔)运算符的组合,并返回TRUE.FALSE或UNKNOWNstart with ...

  8. Oracle 12C -- 统一启动/关闭PDBs

    SQL> select name,open_mode from v$pdbs; NAME OPEN_MODE ------------------------------ ---------- ...

  9. [javase学习笔记]-7.2 构造函数与一般函数的差别

    这一节我们简单学习一下构造函数与一般函数之间的差别所在. 那么它们有什么差别呢,结合上一节,我们能够总结出下面两点差别: 第一个差别: 构造函数:对象创建时,就会调用与之相应的构造函数,对对象进行初始 ...

  10. python ipython spyder

    ipython usage: ipython qtconsole --pylab inline anacond usage: 1. spyder 1. source ~/anacond/bin/act ...