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. poj1284:欧拉函数+原根

    何为原根?由费马小定理可知 如果a于p互质 则有a^(p-1)≡1(mod p)对于任意的a是不是一定要到p-1次幂才会出现上述情况呢?显然不是,当第一次出现a^k≡1(mod p)时, 记为ep(a ...

  2. Git本地版本控制备忘

    首先git是一个版本控制工具,类似于SVN 笔记包括两部分,git本地版本控制和git远程协助 一.Git本地版本控制 以git windows版本msysgit为例,下载地址http://msysg ...

  3. cocos2dx lua 加密

    cocos2dx-lua项目发布时,为了保护lua源码,需要对lua进行加密.通常分为两种方式:加密文件和编译为字节码. 1.加密文件 前提是你不用luajit,而使用lua.这样这种方法是真正加密, ...

  4. AIX下RAC搭建 Oracle10G(一)检測系统环境

    AIX下RAC搭建系列 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 主机名 AIX203 AIX204 交换机 SAN光纤交换机 存储 S ...

  5. android layout属性介绍

    android:id 为控件指定对应的ID android:text 指定控件其中显示的文字,须要注意的是,这里尽量使用strings.xml文件其中的字符串 android:gravity 指定Vi ...

  6. Eclipse中Cannot find any provider supporting DES解决之道

    原文出处:http://blog.csdn.net/darwinchina/article/details/12037999 异常: Caused by: java.security.NoSuchAl ...

  7. python - 类成员修饰符

    在java,c#类的成员修饰符包括,公有.私有.程序集可用的.受保护的. 对于python来说,只有两个成员修饰符:公有成员,私有成员 成员修饰符是来修饰谁呢?当然是修饰成员了.那么python类的成 ...

  8. 如何让div横向排列

    方法一: 一般情况,默认的div是写一个换一行,那么如何定义两个div横向排列而不换行呢? div默认的display属性是block.所以每一个div都是新的一行,现在把display换成inlin ...

  9. (转)SQL中的ISNULL函数介绍

    SQL中有多种多样的函数,下面将为您介绍SQL中的ISNULL函数,包括其语法.注释.返回类型等,供您参考,希望对您学习SQL能够有所帮助. ISNULL 使用指定的替换值替换 NULL. 语法ISN ...

  10. Js 时间间隔计算(间隔天数)

    function GetDateDiff(startDate,endDate)  {      var startTime = new Date(Date.parse(startDate.replac ...