一.题目链接: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. Mysql 创建数据库命令

    GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; UTF8: CREATE DATABASE ` ...

  2. [LeetCode&Python] Problem 563. Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  3. 纯C:AES256

    尼玛的WordPress把格式全搞乱了 aes256.h #ifndef _AES256_H_ #define _AES256_H_ #include <stdio.h> #include ...

  4. linux 用户配制文件 用户增加及删除 以及用户属于的更改

    1.用户密码文件 /etc/passwd root  :   x   :    0    :       0    :          root      :     /root    :    / ...

  5. MySQL数据库-表内容操作

    1.表内容增加 insert into 表 (列名,列名...) values (值,值,值...); 添加表内容添加一条数据 insert into 表 (列名,列名...) values (值,值 ...

  6. hdu3605 Escape 二分图多重匹配/最大流

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...

  7. hdu3488 Tour 拆点+二分图最佳匹配

    In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way r ...

  8. Mybatis(二,三)

    参考孤傲苍狼的博客,地址如下: http://www.cnblogs.com/xdp-gacl/p/4264301.html 在此声明,自己写博客,是为了学习总结过程中的记录.没有侵权和偷懒的意思. ...

  9. 【JVM】参数配置

    [一]JVM参数配置释意 编号 配置项 例子 含义 备注 1 -Xmx -Xmx20m java应用最大可用内存为20M  整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小.持久代一般固 ...

  10. python Console menu

    I just finished a demo which is to provide an easy way to control hardware resources of A sample. Th ...