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 ...
随机推荐
- Sublime 官方安装方法
1. Crtl + ` : 进入控制台模式 2. 复制下面相应版本的代码,按Enter键运行 Sublime Text 3 import urllib.request,os,hashlib; h = ...
- 变量与算术表达式 - C程序设计语言
#include <stdio.h> int main() { float fahr,celsius; float lower,upper,step; lower = 0; upper = ...
- Electron 快速入门
https://www.w3cschool.cn/electronmanual/p9al1qkx.html
- 51Nod 1070:Bash游戏 V4(斐波那契博弈)
1070 Bash游戏 V4 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次拿的数量最少1个 ...
- linux 的IP配置和网络问题的排查
1.6 IP的配制, 首先要会用: ifconfig 和加相关参数如: ifconfig -a, 来查看,自己的电脑网络配制. 再次就必需要知道,默认IP配制文件的地方: cd /etc/sysc ...
- linux下简单制作iso,img镜像文件
1. 如果你是直接从cd压制iso文件的,执行sudo umount /dev/cdromdd if=/dev/cdrom of=file.iso bs=1024 2. 如果你要把某个文件或者目录压到 ...
- linux简单快速启用web
================= jser.me/2013/11/22/快速启动web服务的两种方式.html Python的SimpleHTTPServer需要先安装python,然后执行 pyt ...
- 1.Tensorflow的基本概念:
1.Tensorflow的基本概念: 1.使用图(graphs)来表示计算任务 2.在被称之为会话(Session)的上下文(context)中执行图 3.使用tensor表示数据 4.通过变量(Va ...
- Singer 学习九 运行&&开发taps、targets (四 开发target)
singer 的target 需要从stdin 的行数据,同时处理schema.record.state 消息 指南 schema 需要进行关联stream records 数据的校验 一旦Targe ...
- drone secret 使用
drone 的secret 可以让我们方便的对于需要保密的信息的隐藏,减少账户信息的泄密 环境准备 docker-compose 文件 version: '3' services: drone-s ...