这道题是LeetCode里的第14道题。

题目描述:

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

示例 1:

输入: ["flower","flow","flight"]
输出: "fl"

示例 2:

输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。

说明:

所有输入只包含小写字母 a-z 。

总共有 5 种思路:

  1. 所求的最长公共前缀子串一定是每个字符串的前缀子串,所以随便选择一个字符串作为标准,把它的前缀子串与其它所有字符串进行判断,看是否是它们所有字符串的前缀子串。
  2. 列出所有的字符串的前缀子串,将它们合并后排序,找出其中个数为 n 且最长的子串。
  3. 纵向扫描,从下标 0 开始,逐一判断每个字符串的下标是否相等,直到遇见不全相同的下标位置为止。
  4. 横向扫描:前两个字符串找公共子串,将其结果和第三条字符串同样找公共子串,直到最后一个字符串为止。
  5. 借助 trie 字典树,将这些字符串存储到 trie 树中,那么 trie 树的第一个分叉口之前的单分支树的就是所求。

这里我使用纵向扫描。

解题代码:

class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0)return "";
if(strs.length == 1)return strs[0];
StringBuffer res = new StringBuffer();
StringBuffer shortly = new StringBuffer(strs[0]);
int shortlyIndex = 0;
for(int i=1; i<strs.length; i++) {
if(strs[i].equals(""))return "";
if(shortly.length() > strs[i].length()) {
shortly.delete(0, shortly.length());
shortly.append(strs[i]);
shortlyIndex = i;
}
}
strs[shortlyIndex] = strs[0];
strs[0] = shortly.toString();
//System.out.println(shortly); for(int i=0; i<shortly.length(); i++) {
for(int j=1; j<strs.length; j++) {
if(strs[0].charAt(i) != strs[j].charAt(i)) {
return res.toString();
}
if(j == strs.length - 1) {
res.append(strs[0].charAt(i));
}
}
}
return res.toString();
}
}

提交结果:

个人总结:

题目有坑!strs = [] 的情况万万没想到。而且我这部分代码也是可以简化的,我这样写就是为了避免一些下标的越界错误。但其实没多大的必要。

【LeetCode】Longest Common Prefix(最长公共前缀)的更多相关文章

  1. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  2. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  3. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  4. [LeetCode]14. Longest Common Prefix最长公共前缀

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

  5. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

  6. [leetcode]14. Longest Common Prefix 最长公共前缀

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

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

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

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

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

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

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

随机推荐

  1. Java Object Model(一)

    Java作为OOP语言,抽象性不言而喻.如果需要深入了解Java语言的实现机制,则不得不对Java语言中基础的概念有清晰的了解.今天是我在cnblog上写博客的第一天,希望今天的博客可以是我成为未来& ...

  2. Uva 10635 Prince and Princess (LCS变形LIS)

    直接LCS是时间复杂度是O(p*q)的,但是序列元素各不相同,只要把其中一个序列映射成有序的, 另外一个序列再做相同的映射,没有的直接删掉,就变成了求另一个序列LIS. #include<bit ...

  3. Predicate Programming Guide

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/AdditionalChap ...

  4. [web笔记]解决跨域问题以及axios每次提交session变化的问题

  5. git clone 和 download 不一样,能用git clone 就用git clone,download的代码,经常出现安装bug

    git clone 和 download 不一样,能用git clone 就用git clone,download的代码,经常出现安装bug

  6. 2018.4.16 Java多线程实现龟兔赛跑

    龟兔赛跑(通过多线程来实现 里面的具体方法) TT.java package com.lanqiao.demo3; /** * 乌龟 * @author Administrator * */ publ ...

  7. CentOS安装RabbitMQ步骤

    1.安装gcc yum install gcc 安装 ncurses-devel yum install ncurses-devel 2.安装erlang 下载安装包 http://www.erlan ...

  8. a survey for RL

    • A finite set of states St summarizing the information the agent senses from the environment at eve ...

  9. ovs的学习

    本来编辑好了的, 结果忘了保存, 坑爹,直接把人家的网址贴上来吧 http://blog.chinaunix.net/uid-20737871-id-4333314.html 昨天遇到一个问题(虚拟机 ...

  10. Bootstrap历练实例:默认的媒体对象

    Bootstrap 多媒体对象(Media Object) 本章我们将讲解 Bootstrap 中的多媒体对象(Media Object).这些抽象的对象样式用于创建各种类型的组件(比如:博客评论), ...