这道题是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. 常用css和js组件

    1 . input框中插入图标 <div class="col-sm-12 col-xs-12 setLineHeight"> <div class=" ...

  2. UI设计中蕴涵着系统重要的数据结构与功能设计

    UI设计中蕴涵着系统重要的数据结构与功能设计 UI设计中的用户需求,事件(用例)驱动

  3. Python+selenium整合自动发邮件功能

    主要实现的目的是:自动将测试报告以邮件的形式通知相关人员 from HTMLTestRunner import HTMLTestRunner import HTMLTestReport from em ...

  4. python中的构造函数和构造函数和析构函数的作用

    构造函数和构造函数和析构函数都属于python中的特殊方法 其中的“__del__”就是一个析构函数了,当使用del 删除对象时,会调用他本身的析构函数,另外当对象在某个作用域中调用完毕,在跳出其作用 ...

  5. 洛谷 P3353 在你窗外闪耀的星星

    题目描述 飞逝的的时光不会模糊我对你的记忆.难以相信从我第一次见到你以来已经过去了3年.我仍然还生动地记得,3年前,在美丽的集美中学,从我看到你微笑着走出教室,你将头向后仰,柔和的晚霞照耀着你玫瑰色的 ...

  6. 突然心血来潮,想写写我在java面试中遇到的事。作为一个应届生,我觉得我的情况都与大部分应届生是差不多的,希望你们能在这上面得到一些有用的

    面试过程吧,怎么说呢?从一开始接触面试到现在成功了几家,这中间我确实收获了许多,那我就从我第一次面试开始讲吧. 第一次面试是有人介绍过来的,总之还是有一位贵人相助,所以第一次面试时,面试官很好没有怎么 ...

  7. HDU 4284 Travel (Folyd预处理+dfs暴搜)

    题意:给你一些N个点,M条边,走每条边要花费金钱,然后给出其中必须访问的点,在这些点可以打工,但是需要先拿到证书,只可以打一次,也可以选择不打工之直接经过它.一个人从1号点出发,给出初始金钱,问你能不 ...

  8. Spark Job具体的物理执行

    即使采用pipeline的方式,函数f对依赖的RDD中的数据集合的操作也会有两种方式: 1.f(record),f作用于集合的每一条记录,每次只作用于一条记录 2.f(records),f一次性作用于 ...

  9. c++文件偏移

    #include <iostream> #include <fstream> #include <cassert> using namespace std; int ...

  10. Bootstrap历练实例:向列表组添加内容

    向列表组添加自定义内容 我们可以向上面已添加链接的列表组添加任意的 HTML 内容.下面的实例演示了这点: <!DOCTYPE html><html><head>& ...