当前位置:网站首页>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];
}
边栏推荐
猜你喜欢

机械臂速成小指南(七):机械臂位姿的描述方法

XSS labs master shooting range environment construction and 1-6 problem solving ideas

Read the Flink source code and join Alibaba cloud Flink group..

YYGH-BUG-05

Esp32 audio frame esp-adf add key peripheral process code tracking

HR wonderful dividing line

BEAUTIFUL GGPLOT VENN DIAGRAM WITH R

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement

mysql表的增删改查(进阶)

自然语言处理系列(一)——RNN基础
随机推荐
YYGH-BUG-05
Pytorch builds LSTM to realize clothing classification (fashionmnist)
Those logs in MySQL
Flesh-dect (media 2021) -- a viewpoint of material decomposition
全链路压测
The most understandable f-string tutorial in history, collecting this one is enough
GGPlot Examples Best Reference
xss-labs-master靶场环境搭建与1-6关解题思路
Seriation in R: How to Optimally Order Objects in a Data Matrice
PyTorch nn.RNN 参数全解析
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
mysql索引和事务
From scratch, develop a web office suite (3): mouse events
多文件程序X32dbg动态调试
Log4j2
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
B high and beautiful code snippet sharing image generation
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)
QT meter custom control