一.题目链接:https://leetcode.com/problems/longest-common-prefix/

二.题目大意:

  给定若干个字符串,找出它们的最长公共子串。

三.题解:

  这道题目应该是一道典型题目,需要着重掌握的。该题目的解法并不是很难,代码最好精简化。本题有两种思路:

1.纵向扫描

  任意选出一个字符串(一般都是选择第一个字符串),从该字符串的第一个字符起,与剩余所有的字符串的相应位置的字符比较,若出现相应位置字符不同的情况,则立即返回之前匹配成功的前缀。

代码如下:

class Solution
{
public:
string longestCommonPrefix(vector<string>& strs)
{
int len = strs.size();
if(len == 0)//判断特殊情况
return "";
int s_len = strs[0].size();
for(int i = 0; i < s_len; i++)
for(int j = 1; j < len; j++)
{
if(strs[j][i] != strs[0][i])
return strs[0].substr(0,i);
}
return strs[0];
}
};

该方法有几点需要注意:

(1).特殊情况(即vector为空的情况)一定要考虑(字符串相关的题目,为空的情况是一定要考虑的,切记!),对于每个字符串为空的情况,就不用单独考虑了(在两个for循环中就已经解决这种情况了)。

(2).该算法的时间复杂度为O(n1+n2+...),空间复杂度为O(1).

2.横向扫描

  把所有的字符串与第一个字符串进行比较,选出两者相同的部分,并选取长度最短的部分作为最终结果。这种方法的时间复杂度与纵向扫描在同一数量级的。

LeetCode——14. Longest Common Prefix的更多相关文章

  1. Leetcode 14. Longest Common Prefix(水)

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

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

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

  3. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  4. [LeetCode] 14. Longest Common Prefix

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

  5. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  6. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

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

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

  8. [LeetCode] 14. 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. [LeetCode&Python] Problem 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  2. python 加密 hashlib与hmac模块

    https://www.jb51.net/article/128911.htm hashlib模块简介: hashlib模块为不同的安全哈希/安全散列(Secure Hash Algorithm)和 ...

  3. python三大框架之一(flask介绍)

    Flask , Django,  Tornado 是python中常用的框架,也是python的三大框架.它们的区别是:Flask: 轻量级框架: Django:重量级框架: Tornado:性能最好 ...

  4. plsql developer 使用sys用户登录,报ORA-01031权限不足,解决sys(dba权限用户)远程登录报错。

    数据库服务器架设在机器A上,此时我在机器B上进行远程连接,使用sys账号远程登录时报ORA-01031:insufficient privileges 或 ORA-01017: invalid use ...

  5. struts2参数转换器用法

    贴代码 struts的action中接收简单类型参数,struts2可以自动转化,但是复杂类型需要自定义转换器转换 public class ConvertAction extends ActionS ...

  6. golang-test-tool-gotests

    gotests介绍 gotests是一个Golang命令行工具 ,让Go测试变得容易.它根据目标源文件的函数和方法签名生成表驱动的测试(TDD).任何测试文件中新的依赖都会被自动倒入 Demo 下面是 ...

  7. Map的嵌套使用

    Map嵌套Map: 例: AAA: Javas班: 001 熊大 002 熊二 Hdoop班 001 小猪猪 002 小菲菲 ★使用增强for循环遍历Set数组: import java.util.H ...

  8. HotSpot VM GC 的种类

    collector种类 GC在 HotSpot VM 5.0里有四种: incremental (sometimes called train) low pause collector已被废弃,不在介 ...

  9. goreplay 镜像nginx web app流量

    goreplay 是一个很不错的流量拷贝,复制工具,小巧,支持一些扩展,当然也提供了企业版,企业版 功能更强大,支持二进制协议的分析 备注: 演示使用docker-compose 运行,测试镜像流量到 ...

  10. 【转载】Win10打开U盘提示“文件或目录损坏无法读取”怎么办?

    以下文转载至系统之家 网址:http://www.xitongzhijia.net/xtjc/20190314/152334.html (PS:暂未亲测) Win10打开U盘提示“文件或目录损坏无法读 ...