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 ...
随机推荐
- pytorch实现autoencoder
关于autoencoder的内容简介可以参考这一篇博客,可以说写的是十分详细了https://sherlockliao.github.io/2017/06/24/vae/ 盗图一张,自动编码器讲述的是 ...
- 螺旋图 comet3 (comet) 不同轴的圆周运动图
matlab 绘图 螺旋图小实例 动态显示comet3函数(comet显示平面) t=[:]; x=*t*sin(pi/).*cos(*t); y=*t*sin(pi/).*sin(*t); z=* ...
- Ubuntu防火墙简单设置
http://wiki.ubuntu.org.cn/UFW防火墙简单设置 http://wiki.ubuntu.org.cn/Ufw使用指南 Ubuntu默认安装内置ufw防火墙,简单使用如下: su ...
- EasyUI 文本框回车和普通回车
easyui 回车 $('#Destination_Code').textbox('textbox').bind('keypress', function (e) { ) { } } 普通回车 < ...
- 实验吧—密码学——WP之 杯酒人生
首先我们研究题目 1.这是古典密码 2.喵星人要发送一段密码 3.加密过的秘钥“HTRUZYJW” 4.秘钥加密前是一个单词 我们先解决这个秘钥,用凯撒解密 而我们知道,在古典密码里,有秘钥的加密方式 ...
- MySQL--数据库连接异常问题汇总
======================================================== Name or service not known 错误消息: [Warning] I ...
- 23 模块 os sys pickle json
一. os模块 主要是针对操作系统的 用于文件操作 二. sys 模块 模块的查找路径 sys.path 三 pickle 模块 1. pickle.dumps(对象) 序列化 ...
- IDEA基本設置
2.界面字体大小设置 File菜单->Settings->Appearance->Override default fonts by(not recommended):Name:宋体 ...
- mysql之 mysql_config_editor/login-path 登录密码保护
login-path是MySQL5.6开始支持的新特性.通过借助mysql_config_editor工具将登陆MySQL服务的认证信息加密保存在.mylogin.cnf文件(默认位于用户主目录) . ...
- MyBatis 学习资料
MyBatis 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-typ ...