【LeetCode】最长公共前缀【二分】
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
示例 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】最长公共前缀【二分】的更多相关文章
- leetcode 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串"". 示例 1: 输入: ["flower","flow" ...
- LeetCode 最长公共前缀(探索字节跳动)
题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...
- 每日一道 LeetCode (5):最长公共前缀
前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee: https://gitee.com ...
- LeetCode 14 Longest Common Prefix(最长公共前缀)
题目链接:https://leetcode.com/problems/longest-common-prefix/?tab=Description Problem: 找出给定的string数组中最 ...
- LeetCode:最长公共前缀【14】
LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...
- 【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- 【Leetcode】【简单】【14最长公共前缀】【JavaScript】
题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- LeetCode刷题-最长公共前缀(简单)
题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
随机推荐
- testinfra 基础设施测试工具
testinfra 是基于python 开发的基础设施测试工具,我们可以用来方便的测试基础设施 是否符合我们的要求(系统,软件...) 一个参考demo def test_passwd_file( ...
- shell脚本编程基础之while、for、until循环
while及until循环结构 while CONDITION:do statement done 进入循环:条件满足 退出循环:条件不满足 当需要命令的执行状态返回值时,可以直接把整个命令当做循环的 ...
- kafka 创建消费者报错
kafka-console-consumer.sh --zookeeper master:2181,slave1:2181,slave2:2181 --topic test --from-beginn ...
- spring boot中控制台打印sql日志
.properties文件 logging.level.com.example.demo.dao=debug .yml文件 # 打印sql logging: level: com.example.de ...
- uni-app 模拟器
1.安装MuMu模拟器 Android模拟器端口: 7555 2.安装夜神模拟器 Android模拟器端口: 62001
- mysql .字符串转日期
insert into share (uid, mapId, isdir, type, pwd, shareTime, overTime, price) values (1, 10, 0, 1,&qu ...
- IDEA中设置自动build-改动代码,不用重启工程,刷新页面即可
1.CTRL + SHIFT + A --> 查找Registry --> 找到并勾选compiler.automake.allow.when.app.running 2. FILE ...
- [代码质量] Maintainability Index (MI)
转载自: http://www.projectcodemeter.com/cost_estimation/help/GL_maintainability.htm ProjectCodeMeter Ma ...
- 数据库sql优化总结之4--SQL优化总结
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着数据库中数据的增加,系统 ...
- 纯Python模式
http://crcmod.sourceforge.net/intro.html https://help.aliyun.com/document_detail/85288.html OSS的CRC数 ...