当前位置:网站首页>暴力方法求解(leetcode14)查找字符串数组中的最大公共前缀
暴力方法求解(leetcode14)查找字符串数组中的最大公共前缀
2022-08-02 03:22:00 【寒江飞冰】
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
示例 1:
输入: [“flower”,“flow”,“flight”]
输出: “fl”
示例 2:
输入: [“dog”,“racecar”,“car”]
输出: “”
解释: 输入不存在公共前缀。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string a="";
if (strs.size()== 0) {
return a;
}
for(int i=0;i<strs[0].size();i++) #取数组中的第一个数据进行分析,若后面的数据前缀与第一个前缀不相同或者后面字符串长度达到最大,则break,前面相同的前缀即为数组中字符串的最大前缀,
{
char b=strs[0][i];
int flag=0;
for(int j=1;j<strs.size();j++)
{
if(b!=strs[j][i]||strs[j].size()<=i)
{
flag=1;
break;
}
}
if(flag==0)
{
a=a+b;
}
else{
break;#if flag==1,说明数组中字符串以达到最大前缀,此时直接break
}
}
return a;
}
};
边栏推荐
- 每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch
- STM32 触发HardFault_Handler如何查找原因
- 磷脂-聚乙二醇-醛基 DSPE-PEG-Aldehyde DSPE-PEG-CHO MW:5000
- @Autowired详解[email protected]在static属性上的使用
- 【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?
- Dynamic proxy tool class
- Phospholipid-polyethylene glycol-azide, DSPE-PEG-Azide, DSPE-PEG-N3, MW: 5000
- Debian 10 NTP Service Configuration
- Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
- Monaco Editor 的基本用法
猜你喜欢

远程调试、无cuDnn、自定义模块无法导入问题记录

Usage of JOIN in MySQL
[email protected]在static属性上的使用"/>@Autowired详解[email protected]在static属性上的使用

Debian 10 NTP Service Configuration

磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG

Source Insight 使用教程(2)——常用功能

yolov5调用ip摄像头时出现的问题

磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000

kettle 安装与配置

DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
随机推荐
Chapter 10 Clustering
DSPE-PEG-DBCO 磷脂-聚乙二醇-二苯并环辛炔 一种线性杂双官能聚乙二醇化试剂
Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
远程调试、无cuDnn、自定义模块无法导入问题记录
kettle 安装与配置
云服务器安装部署Nacos2.0.4版本
np.unique()函数
A senior test engineer asked me these questions as soon as the interview came
JJWT tool class
Usage of JOIN in MySQL
String comparison size in MySQL (date string comparison problem)
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
require模块化语法
跨域问题解决
MySQL分区表详解
钟表刻度线
【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
Detailed explanation of the usage of exists in mysql
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记