LeetCode——14. Longest Common Prefix
一.题目链接: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的更多相关文章
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- Java [leetcode 14] Longest Common Prefix
小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...
- Leetcode 14——Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...
- [leetcode]14. Longest Common Prefix 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- [LeetCode] 14. Longest Common Prefix ☆
Write a function to find the longest common prefix string amongst an array of strings. 解法: 广度优先搜索:先比 ...
- [LeetCode]14. Longest Common Prefix最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
随机推荐
- [LeetCode&Python] Problem 13. Roman to Integer
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 多点搜的bfs
Problem L. 跑图Time limit: 1000msMemory limit: 65536KBDescription跑图是 RPG 游戏中很烦躁的事情.玩家需要跑到距离他最近的传送点的位置. ...
- HDU2021发工资咯:)
Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵 但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处 ...
- Quick Noodle Physics in Blender Tutorial
https://www.youtube.com/watch?v=Lg7jxAMs60QQuick Noodle Physics in Blender Tutorial 新增平面Plane作为地面; 新 ...
- cenos7.0 安装docker
使用yum命令在线安装 yum install docker 安装后查看Docker版本 docker -v启动docker:systemctl start docker停止docker:syste ...
- 【shell编程】之基础知识-基本运算符
Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 ...
- oracle Awr报告
Select DBID,INSTANCE_NUMBER,SNAP_ID,TO_CHAR(END_INTERVAL_TIME,'YYYY-MM-DD HH24:MM:SS') AS END_TIME,T ...
- JDBC封装
在模拟servlet调用dao中,我们发现在dao的实现类中有许多重复代码,我们可以将其封装起来. 步骤: 一. 创建一个类 DBUtil 1加载驱动和建立链接的代码 完全一样 加载驱动写到静态代码快 ...
- MySQL--批量KILL连接
============================================== 使用SELECT INTO OUTFILE方式获取到要删除的连接ID并保存为文件,在通过SOURCE执行 ...
- vorpal 又一个方便的cli 开发包
vorpal 是一个npm 包,我们可以用来开发专业的cli 程序 简单使用 初始化项目 yarn init -y 添加依赖 yarn add vorpal 简单demo app.js // cons ...