当前位置:网站首页>Leetcode14 longest public prefix
Leetcode14 longest public prefix
2022-07-02 12:06:00 【Monsters 114】
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string
""
.Input :strs = ["flower","flow","flight"] Output :"fl"
Ideas : Longitudinal scan
During longitudinal scanning , Go through each column of all strings from front to back , Compare whether the characters on the same column are the same , If the same, continue to compare the next column , If not, the current column no longer belongs to the public prefix , When the part before the front row is the longest public prefix .
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0)
return "";
for(int i = 0; i < strs[0].length();i++){
char c = strs[0].charAt(i);
for(int j = 1; j < strs.length;j++){
// The length of other strings is less than the length of the first string
if(i == strs[j].length() || strs[j].charAt(i) != c)
return strs[0].substring(0,i);
}
}
return strs[0];
}
边栏推荐
- 输入一个三位的数字,输出它的个位数,十位数、百位数。
- HR wonderful dividing line
- Mish shake the new successor of the deep learning relu activation function
- FLESH-DECT(MedIA 2021)——一个material decomposition的观点
- Fabric.js 3个api设置画布宽高
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- 【C语言】十进制数转换成二进制数
- 倍增 LCA(最近公共祖先)
- 史上最易懂的f-string教程,收藏这一篇就够了
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
猜你喜欢
YYGH-BUG-05
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
HOW TO ADD P-VALUES TO GGPLOT FACETS
PyTorch搭建LSTM实现服装分类(FashionMNIST)
Mish-撼动深度学习ReLU激活函数的新继任者
Take you ten days to easily finish the finale of go micro services (distributed transactions)
From scratch, develop a web office suite (3): mouse events
Natural language processing series (I) -- RNN Foundation
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
倍增 LCA(最近公共祖先)
随机推荐
Log4j2
PyTorch中repeat、tile与repeat_interleave的区别
Develop scalable contracts based on hardhat and openzeppelin (I)
Natural language processing series (II) -- building character level language model using RNN
高德地图测试用例
Power Spectral Density Estimates Using FFT---MATLAB
pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
From scratch, develop a web office suite (3): mouse events
小程序链接生成
YYGH-BUG-04
【2022 ACTF-wp】
Codeforces 771 div2 B (no one FST, refers to himself)
Read the Flink source code and join Alibaba cloud Flink group..
Mish shake the new successor of the deep learning relu activation function
Yygh-9-make an appointment to place an order
CONDA common command summary
Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)
[geek challenge 2019] upload
PyTorch nn.RNN 参数全解析
测试左移和右移