Question

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

Solution

第一思路是用Trie,但是对于一道Easy类别的题目,用Trie显然是杀鸡用牛刀。

于是我们可以参考到构造Trie的过程,来找这个最长prefix。

我们先用String array中第一个String初始化Pre,然后遍历剩下的每一个String,找重合的终点,删除不重合的部分。

 public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length < 1) {
return "";
}
int length = strs.length;
int initLen = strs[0].length();
StringBuilder result = new StringBuilder(initLen);
for (int i = 0; i < initLen; i++) {
result.append(strs[0].charAt(i));
}
for (int i = 1; i < length; i++) {
String curString = strs[i];
initLen = result.length();
for (int j = 0; j < initLen; j++) {
if (j < curString.length() && curString.charAt(j) == result.charAt(j)) {
continue;
}
result.delete(j, initLen);
break;
}
}
return result.toString();
}
}

更简单的方法是用String.indexOf()的方法来判断重合终点。

 public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length < 1) {
return "";
}
int length = strs.length;
int initLen = strs[0].length();
String prev = strs[0];
for (int i = 0; i < length; i++) {
String curString = strs[i];
while (curString.indexOf(prev) != 0) {
prev = prev.substring(0, prev.length() - 1);
}
}
return prev;
}
}

Longest Common Prefix 解答的更多相关文章

  1. LeetCodeOJ刷题之14【Longest Common Prefix】

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

  2. Longest Common Prefix -最长公共前缀

    问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...

  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. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

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

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

  7. 14. Longest Common Prefix

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

  8. Leetcode Longest Common Prefix

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

  9. [LeetCode] 14. Longest Common Prefix

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

随机推荐

  1. C++简介

    本文仅用于学习交流,转载请注明:http://www.cnblogs.com/mxbs/p/6266466.html  Hello,C++ World! 简介: C++融合了3中不同的编程传统:C语言 ...

  2. [Node.js] Using ES6 and beyond with Node.js

    If you're used to using all the latest ES6+ hotness on the front end via Babel, working in Node.js c ...

  3. [Javascript] Advanced Reduce: Composing Functions with Reduce

    Learn how to use array reduction to create functional pipelines by composing arrays of functions. co ...

  4. 图像重采样(CPU和GPU)

    1 前言 之前在写影像融合算法的时候,免不了要实现将多光谱影像重采样到全色大小.当时为了不影响融合算法整体开发进度,其中重采样功能用的是GDAL开源库中的Warp接口实现的. 后来发现GDAL War ...

  5. C#基础:集合

        C#中的数组实现为 System.Array 类的实例,它们只是集合类(Collection Classes)中的一种类型.集合类一般用于处理对象列表,其功能比简单数组要多,功能大多是通过实现 ...

  6. highcharts的使用

    步骤: 1. 去highcharts官网下载最新版本 2. 在.aspx页面添加引用 例: <link href="../JS/highcharts/css/highslide.css ...

  7. VS2010不能打开预编译的网站源码的原因是什么?(转之csdn)

    原问题: 今天将写好的一个网站源码目录拷贝到另一台电脑上,但打开时提示:    你要打开一个预编译的网站,你可以查看该站点,但对它进行更改可能会造成该网站停止运行,若要修改站点,建议先编辑原始网站中的 ...

  8. 一个Banner广告收缩效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. PDO LIMIT bug

    PDO存在一个LIMIT BUG(mysql) 需要指定数据类型,而且limit后面跟的2个参数必须是数值类型,不然的话获取不到数据 例1: $dsn = "mysql:host=127.0 ...

  10. Tomcat unable to start

    在学习springMvc时,导入springfreemarker 的jar包,写好web.xml,config.xml 后. 部署到tomcat,异常如下: 八月 27, 2016 5:44:41 下 ...