当前位置:网站首页>暴力方法求解(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-thiol, DSPE-PEG-Thiol, DSPE-PEG-SH, MW: 5000
- np.isnan()
- DOM操作---放大镜案例
- [Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
- C语言的变长数组
- ssm各类配置模板
- SSM integration
- 知识问答与知识会话的区别
- 小程序 van-cell 换行能左对齐问题
- canvas--饼状图
猜你喜欢

Amazon sellers how to improve the conversion

AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'

排序学习笔记(二)堆排序

canvas--饼状图

parser = argparse.ArgumentParser()解析

Phospholipid-polyethylene glycol-azide, DSPE-PEG-Azide, DSPE-PEG-N3, MW: 5000

nucleo stm32 h743 FREERTOS CUBE MX配置小记录

UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu

C语言的变长数组

oracle inner join and outer join
随机推荐
PCL—点云数据分割
sh: 1: curl: not found
相对路径和绝对路径
【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?
ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全
Redis simple study notes
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶供应,MW:5000
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
MySQL分页查询的5种方法
@DateTimeFormat注解
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/yolov5-5.0/models/commo
ModuleNotFoundError No module named 'xxx' possible solutions
如何查看一个现有的keil工程之前由什么版本的keil IDE编译
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
@Autowired与@Resource区别
通过PS 2021 将网页图标抠下来
COCO数据集训练TPH-YoloV5
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
远程调试、无cuDnn、自定义模块无法导入问题记录