这道题是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. GitHub 开启 Two-factor authentication,如何在命令行下更新和上传代码

    最近在使用GitHub管理代码,在git命令行管理代码时候遇到一些问题. 如果开起了二次验证(Two-factor authentication两个要素认证),命令行会一直提示输入用户名和密码.查找了 ...

  2. Easyui validatebox后台服务端验证

    Easyui validatebox的验证提示十分好用,可是在实际项目的运用中,经常会遇到需要服务器验证后并返回验证结果信息,比如验证用户名.手机号.邮箱是否已存在.于是就想着怎么拓展Easyui的验 ...

  3. 【虚拟机-网关】如何在使用应用程序网关和 Nginx 的环境下实现强制 HTTPS 跳转

    背景介绍 大家在使用 Nginx 部署网站时,实现 HTTP 到 HTTPS 的强制跳转是非常容易的事情,一般可以使用rewrite 命令或者使用返回自定义 301 页面的方法对 HTTP 请求进行 ...

  4. allure使用简介

    #安装依赖包pip install requests_toolbeltpip install pyyamlpip install pytest-allure-adaptor #安装allure2 说明 ...

  5. Monitorix系统和网络监控工具

    Monitorix 系统和网络监控公工具一.monitorixMonitorix是一款功能非常强大的免费开源轻型工具,目的在于监测Linux中的系统和网络资源.它可以定期收集系统和网络数据,并使用自己 ...

  6. dp cf 1700 最近几天的刷题

    C. Number of Ways 这个题目的意思是,把这个n的序列分成三个连续的部分,要求这三个部分的和是一样的.问这种划分的方法有多少种. 这个题目和之前写过的数字划分有点像,这个就是要先进行前缀 ...

  7. python_106_创建类的两种方式

    class Foo(object): def __init__(self, name): self.name = name f = Foo("alex") print(type(f ...

  8. kubernetes监控-prometheus(十六)

    监控方案 cAdvisor+Heapster+InfluxDB+Grafana Y 简单 容器监控 cAdvisor/exporter+Prometheus+Grafana Y 扩展性好 容器,应用, ...

  9. iOS应用架构谈-part1概述

    当我们讨论客户端应用架构的时候,我们在讨论什么? 其实市面上大部分应用不外乎就是颠过来倒过去地做以下这些事情: --------------- --------------- ------------ ...

  10. MySQL数据库的多种备份与多种还原

    一.备份 1.mysqldump 方法备份 mysqldump备份很简单,格式如下: mysqldump -u用户名 -p密码 数据库名> XX.sql 路径 例如: mysqldump -ur ...