当前位置:网站首页>Longest public prefix of leetcode
Longest public prefix of leetcode
2022-06-24 09:16:00 【SZU healing system bug】
Title Description
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string .
Thought analysis
The longest public prefix is first public , That means everyone has , So we can get a string first , Then compare from beginning to end , That's exactly what it is : Used to take the first one to operate , Compare the first string with the following string , Compare character by character , If you encounter different characters, it means that the same characters have disappeared , It's over , If they are the same , So the longest public is yourself .
AC Code
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];
}
};边栏推荐
- [Niuke] length of the last word of HJ1 string
- Support vector machine (SVC, nusvc, linearsvc)
- Essay - Reflection
- Lu Qi: I am most optimistic about these four major technology trends
- 4274. suffix expression
- Microblog writing - flow chart - sequence chart - Gantt chart - Mermaid flow chart - good results
- PM2 deploy nuxt3 JS project
- The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different
- Xiaobai needs to learn MySQL - incremental statistical SQL
- Chapter 7 operation bit and bit string (III)
猜你喜欢

普通人没有学历,自学编程可以月入过万吗?

CDGA|到底怎么才能做好数据治理呢?

Zero foundation self-study SQL course | having clause
Depens:*** but it is not going to be installed

Ordinary people have no education background. Can they earn more than 10000 yuan a month by Self-taught programming?

Mba-day25 best value problem - application problem

Linux MySQL installation

华为路由器:ipsec技术

十二、所有功能实现效果演示

【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
随机推荐
Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读
Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"
linux(centos7.9)安装部署mysql-cluster 7.6
Data middle office: detailed explanation of technical architecture of data middle office
【LeetCode】541. Reverse string II
【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
每周推薦短視頻:談論“元宇宙”要有嚴肅認真的態度
Depens:*** but it is not going to be installed
From the Huawei weautomate digital robot forum, we can see the "new wisdom of government affairs" in the field of government and enterprises
Huawei Router: GRE Technology
CDGA|到底怎么才能做好数据治理呢?
华为路由器:GRE技术
2022.06.23 (traversal of lc_144,94145\
MySQL | view notes on Master Kong MySQL from introduction to advanced
从618看京东即时零售的野心
EasyExcel单sheet页与多sheet页写出
LeetCode之最长公共前缀
2021-05-20computed and watch applications and differences
leetcode——错误的集合
读CVPR 2022目标检测论文得到的亿点点启发