当前位置:网站首页>暴力方法求解(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;
}
};
边栏推荐
- MySQL中字符串比较大小(日期字符串比较问题)
- __dirname
- 磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000
- 解决glob()返回文件排序不一致问题&onnx本地按照安装方法
- 这些JS题面试时一定要答对!
- Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
- MySQL占用CPU过高,排查原因及解决的多种方式法
- Deveco studio Hongmeng app access network detailed process (js)
- PCL—point cloud data segmentation
- ssm各类配置模板
猜你喜欢
Deveco studio 鸿蒙app访问网络详细过程(js)
化学试剂磷脂-聚乙二醇-羟基,DSPE-PEG-OH,DSPE-PEG-Hydroxyl,MW:5000
yolov5调用ip摄像头时出现的问题
【装机】老毛桃的安装及使用
Phospholipid-polyethylene glycol-azide, DSPE-PEG-Azide, DSPE-PEG-N3, MW: 5000
STM32 map文件解析
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
【深度学习】从LeNet-5识别手写数字入门深度学习
parser = argparse.ArgumentParser()解析
kettle 安装与配置
随机推荐
sh: 1: curl: not found
Debian 10 NTP Service Configuration
Monaco Editor 的基本用法
COCO数据集训练TPH-YoloV5
【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?
解决glob()返回文件排序不一致问题&onnx本地按照安装方法
MySQL分页查询的5种方法
网址URL
The usage of json type field in mysql
@Autowired详解[email protected]在static属性上的使用
Phospholipid-polyethylene glycol-azide, DSPE-PEG-Azide, DSPE-PEG-N3, MW: 5000
STM32 map文件解析
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
Source Insight 使用教程(2)——常用功能
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000
getattr()函数解析
【面经】米哈游数据开发一面二面面经
Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
MySql创建数据表
C语言入门小游戏—三子棋