当前位置:网站首页>暴力方法求解(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;
}
};
边栏推荐
猜你喜欢
【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
新工程加载YOLOV6的预训练权重问题
canvas--饼状图
第一篇博客
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
亚马逊卖家怎么提升转化率
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000
网址URL
随机推荐
化学试剂磷脂-聚乙二醇-羟基,DSPE-PEG-OH,DSPE-PEG-Hydroxyl,MW:5000
Cloud server installation and deployment of Nacos 2.0.4 version
PCL—point cloud data segmentation
The @autowired distinguished from @ the Resource
MySQL分页查询的5种方法
A senior test engineer asked me these questions as soon as the interview came
利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置
JJWT工具类
亚马逊卖家怎么提升转化率
oracle inner join and outer join
DOM操作---放大镜案例
ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全
@Autowired与@Resource区别
npm和package.json
[Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
MySQL分组后排序
C语言 内联函数
meime模块
Detailed explanation of the usage of exists in mysql
云服务器安装部署Nacos2.0.4版本