当前位置:网站首页>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];
}
边栏推荐
- Research on and off the Oracle chain
- HOW TO ADD P-VALUES TO GGPLOT FACETS
- Some problems encountered in introducing lvgl into esp32 Arduino
- Pytorch builds LSTM to realize clothing classification (fashionmnist)
- GGPlot Examples Best Reference
- php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- 文件操作(详解!)
- YYGH-10-微信支付
- How to Easily Create Barplots with Error Bars in R
猜你喜欢
自然语言处理系列(一)——RNN基础
How to Create a Nice Box and Whisker Plot in R
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
Seriation in R: How to Optimally Order Objects in a Data Matrice
Mish-撼动深度学习ReLU激活函数的新继任者
Pytorch builds LSTM to realize clothing classification (fashionmnist)
Flesh-dect (media 2021) -- a viewpoint of material decomposition
自然语言处理系列(三)——LSTM
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
【2022 ACTF-wp】
随机推荐
进入前六!博云在中国云管理软件市场销量排行持续上升
File operation (detailed!)
Mish shake the new successor of the deep learning relu activation function
This article takes you to understand the operation of vim
Power Spectral Density Estimates Using FFT---MATLAB
[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)
PHP 2D and multidimensional arrays are out of order, PHP_ PHP scrambles a simple example of a two-dimensional array and a multi-dimensional array. The shuffle function in PHP can only scramble one-dim
conda常用命令汇总
Develop scalable contracts based on hardhat and openzeppelin (I)
Principle of scalable contract delegatecall
B high and beautiful code snippet sharing image generation
Writing contract test cases based on hardhat
SCM power supply
YYGH-10-微信支付
The selected cells in Excel form have the selection effect of cross shading
Le tutoriel F - String le plus facile à comprendre de l'histoire.
GGPlot Examples Best Reference
Depth filter of SvO2 series
The position of the first underline selected by the vant tabs component is abnormal
Seriation in R: How to Optimally Order Objects in a Data Matrice