[抄题]:

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

在 "ABCD" "ABEF" 和 "ACEF" 中,  LCP 为 "A"

在 "ABCDEFG", "ABCEFG", "ABCEFA" 中, LCP 为 "ABC"

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

字符串数组为空、长度为0的情况都需要考虑

[思维问题]:

知道要从最短的前缀开始找,还以为要排序

[一句话思路]:

每个单词都用.indexof()逐个压缩前缀

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

对前缀字符串进行压缩

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  • int indexOf(String str): 返回指定字符串在字符串中第一次出现处的索引,如果此字符串中没有这样的字符串,则返回 -1。

若strs[i].indexOf(pre) == ,则有此前缀。这是判断前缀的新方法。

[关键模板化代码]:

每个单词都要做前缀压缩

for (int i = 1; i < n; i++) {
while (strs[i].indexOf(pre) != 0) {
pre = pre.substring(0, pre.length() - 1);
}
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

public class Solution {
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
public String longestCommonPrefix(String[] strs) {
//corner case
if (strs == null) {
return "";
}
if (strs.length == 0) {
return "";
}
//define pre
String pre = strs[0];
int n = strs.length;
//shorten pre
for (int i = 1; i < n; i++) {
while (strs[i].indexOf(pre) != 0) {
pre = pre.substring(0, pre.length() - 1);
}
}
//return
return pre;
}
}

14. Longest Common Prefix 最长的公共字符串开头的更多相关文章

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

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

  2. 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 ...

  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] 14. Longest Common Prefix 最长共同前缀

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

  6. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  7. 14. Longest Common Prefix【leetcode】

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

  8. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  9. Leetcode 14. Longest Common Prefix(水)

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

随机推荐

  1. java指定文件编码格式

    在创建文件并打印字符串时,如果不指定编码,默认是按系统的编码格式来.比如我们的linux环境中编码如下: CMREAD-SV43 /home/wlf> locale LANG=en_US.UTF ...

  2. 使用zabbix监控mysql

    系统版本: centos6 x86_64 mysql版本: mysql5.6 实施目的: 监控mysql 客户端配置: 1.准备工作:搭建zabbix服务,使服务端客户端连接成功,并有基础监控项 2. ...

  3. 使用Nancy搭建简单的Http服务的示例demo

    刚刚接触Nancy没几天,暂时还不会使用Nancy来做web开发,只是使用Nancy实现了一个简单的Http服务的Demo程序,实现对Post和Get请求的处理. Demo的示例代码地址如下:http ...

  4. Go的List操作上的一个小“坑”

    转自http://sharecore.net/blog/2014/01/09/the-trap-in-golang-list/ 一直想不清楚一个问题,简单设计的东西到底是“坑多”还是“坑少”呢? 复杂 ...

  5. linux下mysql提示"mysql deamon failed to start"错误的解决方法

    操作系统为centos,网站突然连接不上数据库,于是朋友直接重启了一下服务器.进到cli模式下,执行 service myqsld start 发现还是提示"mysql deamon fai ...

  6. mysql 下字符集知识汇总

    Do not issue the query set names with Connector/J, as the driver will not detect that the character ...

  7. 全连接BP神经网络

    前馈神经网络 前馈神经网络(feedforward neural network)是最朴素的神经网络,通常我们所说的前馈神经网络有两种,一种叫反向传播网络(Back propagation Netwo ...

  8. JAVA构造函数在超类与子类定义鲁波总结

    1.子类无构造函数,超类无构造函数,创建的无参数的对象: 编译通过. class A { } class B extends A { } public class Testeeer { public ...

  9. SQL、PL/SQL、DDL、DML、TCL介绍

    SQL:结构化查询语言(Structured Query Language) PL/SQL:过程化SQL语言(Procedural Language/SQL) DDL(Data Definition ...

  10. SSMS安装英文版后无法修改为中文

    SSMS的UI语言和所安装的Visual Studio的语言是相关的,你这种情况应该是第一次安装的时候安装了英文版的visual studio isolated shell,在卸载的时候你没有卸载这个 ...