当前位置:网站首页>LeetCode之最长公共前缀
LeetCode之最长公共前缀
2022-06-24 07:54:00 【SZU治愈系BUG】
题目描述
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串。
思路分析
最长公共前缀首先是公共的,这意味大家都有,那么我们可以先拿一个字符串出来,然后从头比较到尾,具体就是这样:习惯拿第一个来操作,让第一个字符串和后面的字符串比较,一个字符一个字符地比较,碰到不相同的说明大家相同的字符已经没了,立马结束,如果都相同,那么说明最长的公共就是自己。
AC代码
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int num=strs.size();
for(int i=0;strs[0][i];i++)
{
for(int j=1;j<num;j++)
{
if(strs[0][i]!=strs[j][i])
return strs[0].substr(0,i);
}
}
return strs[0];
}
};边栏推荐
- 学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
- Data middle office: the data middle office practice scheme of Minsheng Bank
- 荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
- Applet cloud data, data request a method to collect data
- 2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
- [MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle
- Huawei Router: IPSec Technology
- Applet wx show
- 阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
- linux(centos7.9)安装部署mysql-cluster 7.6
猜你喜欢

On the routing tree of gin

目标检测系列——Fast R-CNN

Leetcode -- wrong set

Ebanb B1 Bracelet brush firmware abnormal interrupt handling

Qingcloud based "real estate integration" cloud solution

Data midrange: detailed explanation of the technical stack of data acquisition and extraction

Kaformer personal notes

【使用 PicGo+腾讯云对象存储COS 作为图床】

Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?

Opencv maximum filtering (not limited to images)
随机推荐
2022.6.13-6.19 AI行业周刊(第102期):职业发展
tcpdump抓包实现过程
2020 China's provinces and cities, three-level linkage data, data agencies (data from the official website of the National Bureau of Statistics)
小白学习MySQL - 增量统计SQL的需求
MYCAT read / write separation and MySQL master-slave synchronization
The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
Depens:*** but it is not going to be installed
Lu Qi: I am most optimistic about these four major technology trends
Every (), map (), forearch () methods. There are objects in the array
Kaformer personal notes
【LeetCode】541. Reverse string II
Double pointer analog
关于 GIN 的路由树
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
华为路由器:ipsec技术
解决:jmeter5.5在win11下界面上的字特别小
PM2 deploy nuxt3 JS project
Data middle office: overview of data governance
金仓KFS replicator安装(Oracle-KES)
Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment