当前位置:网站首页>暴力方法求解(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;
}
};
边栏推荐
- 广州华为面试总结
- Phospholipid-polyethylene glycol-azide, DSPE-PEG-Azide, DSPE-PEG-N3, MW: 5000
- DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶供应,MW:5000
- Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
- Usage of JOIN in MySQL
- COCO数据集训练TPH-YoloV5
- yolov5调用ip摄像头时出现的问题
- debian 10 nat 与路由转发
- 知识工程作业2:知识工程相关领域介绍
- MySQL分库分表
猜你喜欢
随机推荐
SOCKS5
basic operator
磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
ModuleNotFoundError No module named 'xxx' possible solutions
C语言入门小游戏—三子棋
磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000
小程序 van-cell 换行能左对齐问题
display,visibility,opacity
客户评分控件
sh: 1: curl: not found
parser = argparse.ArgumentParser()解析
【我的创作纪念日】 3周年
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
mysql中如何查看表是否被锁
如何查看一个现有的keil工程之前由什么版本的keil IDE编译
canvas--饼状图
通过PS 2021 将网页图标抠下来
np.unique() function









