【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 ...
随机推荐
- intellij idea 搜索快捷键
Ctrl+N按名字搜索类 1 相当于eclipse的ctrl+shift+R,输入类名可以定位到这个类文件 2 就像idea在其它的搜索部分的表现一样,搜索类名也能对你所要搜索的内容多个部分进行匹配 ...
- LeetCode 1004. Max Consecutive Ones III
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...
- WinDbg常用命令系列---反汇编u*
u, ub, uu (Unassemble) u*命令显示内存中指定程序代码的汇编转换.不要将此命令与~u(解冻线程)命令混淆. u[u|b] Range u[u|b] Address u[u|b] ...
- 21-ESP8266 SDK开发基础入门篇--C# TCP客户端 , 控制LED亮灭
https://www.cnblogs.com/yangfengwu/p/11192603.html 由于是台式机,,没有插无线网卡...所以呢我就用调试助手监控下数据 后期让WIFI连接路由器的时候 ...
- 10-ESP8266 SDK开发基础入门篇--上位机通过串口控制ESP8266灯亮灭
https://www.cnblogs.com/yangfengwu/p/11087618.html 其实这一节就是对上三节的综合测试 https://www.cnblogs.com/yangfeng ...
- nodejs+nvm历史版本
官网:http://nodejs.org/dist/ 淘宝镜像:https://npm.taobao.org/mirrors/node/ nvm历史版本:https://github.com/core ...
- CMD下文件操作
CMD下常用文件操作指令 1.输入盘符 如C: 然后回车,相当于进了C盘(c盘一般进的是桌面目录) 2.输入 cd 目录名 然后回车(目录名是c盘中的一级目录名,也可为c盘中的目录路径).如cd mm ...
- js中forEach,for in,for of循环的用法详解
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...
- Kubernetes kubectl 命令概述
kubectl用于运行Kubernetes集群命令的管理工具. 语法 kubectl [command] [TYPE] [NAME] [flags] command:指定要在一个或多个资源执行的操作 ...
- vscode前端插件(我的标配)
前言 今天给我的vscode编辑汉化了一下)我也不知道为什么要汉化一下... 但是汉化后 我的vue文件木有高亮了,2333(只好一顿操作 再安装插件 还要去百度找 自己留存) 汉化后 是所有的插件都 ...