当前位置:网站首页>Leetcode14 最长公共前缀
Leetcode14 最长公共前缀
2022-07-02 09:42:00 【魑魅魍魉114】
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串
""。输入:strs = ["flower","flow","flight"] 输出:"fl"
思路:纵向扫描
纵向扫描时,从前往后遍历所有字符串的每一列,比较相同列上的字符是否相同,如果相同则继续对下一列进行比较,如果不相同则当前列不再属于公共前缀,当前列之前的部分为最长公共前缀。
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++){
//其余字符串长度小于第一个字符串长度
if(i == strs[j].length() || strs[j].charAt(i) != c)
return strs[0].substring(0,i);
}
}
return strs[0];
}
边栏推荐
- php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
- Mish shake the new successor of the deep learning relu activation function
- ESP32存储配网信息+LED显示配网状态+按键清除配网信息(附源码)
- [visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
- Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
- 动态内存(进阶四)
- uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
- Summary of flutter problems
- [multithreading] the main thread waits for the sub thread to finish executing, and records the way to execute and obtain the execution result (with annotated code and no pit)
- easyExcel和lombok注解以及swagger常用注解
猜你喜欢

H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser

Data analysis - Matplotlib sample code

Depth filter of SvO2 series

How to Add P-Values onto Horizontal GGPLOTS

Deep understanding of NN in pytorch Embedding

深入理解P-R曲线、ROC与AUC

GGPlot Examples Best Reference

【2022 ACTF-wp】

Power Spectral Density Estimates Using FFT---MATLAB

File operation (detailed!)
随机推荐
Seriation in R: How to Optimally Order Objects in a Data Matrice
HOW TO ADD P-VALUES TO GGPLOT FACETS
Principe du contrat évolutif - delegatecall
【2022 ACTF-wp】
小程序链接生成
Precautions for scalable contract solution based on openzeppelin
Mish-撼动深度学习ReLU激活函数的新继任者
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
预言机链上链下调研
文件操作(详解!)
Principle of scalable contract delegatecall
Log4j2
How does Premiere (PR) import the preset mogrt template?
Mish shake the new successor of the deep learning relu activation function
史上最易懂的f-string教程,收藏这一篇就够了
Log4j2
How to Easily Create Barplots with Error Bars in R
A sharp tool for exposing data inconsistencies -- a real-time verification system
vant tabs组件选中第一个下划线位置异常
QT meter custom control