编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""。

示例 1:

输入: ["flower","flow","flight"]
输出: "fl"

示例 2:

输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。

说明:

所有输入只包含小写字母 a-z 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-common-prefix

 

分析:

方法1:找到最短的的字符串,然后拿最短字符串的每个字母去所有字符串中验证(验证该字符在每个字符串中都存在)

时间复杂度:O(S),S为所有字符串中字符数量的总和

最好情况:O(N*minL),N为字符串数量,minl为最短字符串的数量

最坏情况:O(N*M)(N个长度为M的相同字符串)

空间复杂度:O(1)

 

方法2:找到最短字符串,然后对最短字符串的长度进行二分,每次得到长度mid,就去验证长度mid是否符合要求(验证方法:看看所有字符串的前mid个字符是否相同)

时间复杂度:O(S*log(minl)),S为所有字符串的字符数量,minl为最短字符串的长度

空间复杂度:O(1)

 

方法1:

class Solution {
public:
string longestCommonPrefix(vector<string>& v)
{
int n=v.size();
if(n==)
return "";
if(n==)
return v[];
int minl=INT_MAX;
int index=;
for(int i=;i<n;i++)
{
int k=v[i].size();
if(k<minl)
{
minl=k;
index=i;
}
}
string ans;
for(int j=;j<=minl;j++)
{
for(int i=;i<n;i++)
{
if(v[i][j]==v[index][j])
continue;
else
return ans;
}
ans+=v[index][j];
}
return ans;
}
};

方法2:

class Solution {
public:
string longestCommonPrefix(vector<string>& v)
{
int n=v.size();
if(n==)
return "";
if(n==)
return v[];
if(n==&&v[]==v[])
return v[];
int minl=v[].length();
for(int i=;i<n;i++)
{
int k=v[i].size();
if(v[i][]!=v[i-][])
return "";
if(k==)
{
return "";
}
if(k<minl)
{
minl=k;
}
}
string ans=v[].substr(,);
int start=;
int end=minl;
while(start<=end)
{
int mid=(start+end)/;
string str=v[]; int flag=;
for(int i=;i<n;i++)
{
if(str.substr(,mid)!=v[i].substr(,mid))
{
flag=;
break;
}
}
if(flag==)
{
start=mid+;
ans=str.substr(,mid);
}else
{
end=mid-;
}
}
return ans;
}
};

 

【LeetCode】最长公共前缀【二分】的更多相关文章

  1. leetcode 最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串"". 示例 1: 输入: ["flower","flow" ...

  2. LeetCode 最长公共前缀(探索字节跳动)

    题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

  3. 每日一道 LeetCode (5):最长公共前缀

    前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee: https://gitee.com ...

  4. LeetCode 14 Longest Common Prefix(最长公共前缀)

    题目链接:https://leetcode.com/problems/longest-common-prefix/?tab=Description   Problem: 找出给定的string数组中最 ...

  5. LeetCode:最长公共前缀【14】

    LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...

  6. 【LeetCode】Longest Common Prefix(最长公共前缀)

    这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...

  7. 【Leetcode】【简单】【14最长公共前缀】【JavaScript】

    题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

  8. LeetCode刷题-最长公共前缀(简单)

    题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

  9. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

随机推荐

  1. 题解 LA2889

    题目大意 多组数据,每组数据给出一个正整数 \(n\),输出第 \(n\) 大的回文数(即 \(1,2,3,\cdots\)). 分析 不难发现,\(n\) 位的回文数有 \(9*10^{\lfloo ...

  2. Effective C++ 读后感笔记

    1.赋值不一定是初始化.例如 AClassName::AClassName(const std::string &name, const std::string &address, c ...

  3. LeetCode 740. Delete and Earn

    原题链接在这里:https://leetcode.com/problems/delete-and-earn/ 题目: Given an array nums of integers, you can ...

  4. WinDbg常用命令系列---日志操作相关命令log*

    .logopen (Open Log File) .logopen命令将事件和命令的副本从调试器命令窗口发送到新的日志文件. .logopen [Options] [FileName] .logope ...

  5. nexus 3.17.0 简单试用

    老样子,使用docker-compose 运行 环境准备 docker-compose 文件 version: "3" services: nexus: image: sonaty ...

  6. Ubuntu下设置 nginx php-fpm 自动启动 rc.local

    编辑 root@ubuntu:/usr/sbin# vim /etc/init.d/rc.local /usr/sbin/php-fpm /usr/sbin/nginx 保存!

  7. Zotero使用教程(2)-数据备份

    小书匠 这篇文章的目标是让你无论是 换系统,重新安装zotero等都可以还原回你的文献库,而且整个过程基本是自动完成的. 这部分解决下面的两种情况: 1.zotero有自己既定的一套存储方式,不是一般 ...

  8. Mysql索引面试题

    转载:https://mp.weixin.qq.com/s/_bk2JVOm2SkXfdcvki6-0w 本文来自一位不愿意透露姓名的粉丝投稿,由Hollis整理并"还原"了面试现 ...

  9. 剑指offer:字符流中第一个不重复的字符

    题目描述: 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读 ...

  10. 请写出css3样式的优先级,并排序

    !important(权重最大)1000>内嵌样式(style="")>内部样式>外联样式>@import url("url");