当前位置:网站首页>leetcode006--查找字符串数组中的最长公共前缀
leetcode006--查找字符串数组中的最长公共前缀
2022-04-23 04:38:00 【singularityDZF】
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test06 {
/**
* 编写一个函数来查找字符串数组中的最长公共前缀。
* 如果不存在公共前缀,返回空字符串""。
*
* 示例 1:
* 输入:strs = ["flower","flow","flight"]
* 输出:"fl"
*
* 示例 2:
* 输入:strs = ["dog","racecar","car"]
* 输出:""
* 解释:输入不存在公共前缀。
*
* 提示:
* 1 <= strs.length <= 200
* 0 <= strs[i].length <= 200
* strs[i] 仅由小写英文字母组成
*
* @param args
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] strs = null;
strs = br.readLine().split(",");
System.out.println(longestCommonPrefix(strs));
}
public static String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0){
return "";
}
int length = strs[0].length();
int count = strs.length;
for(int i=0; i<length; i++){
char c = strs[0].charAt(i);
for(int j=1; j<count; j++){
if(i == strs[j].length() || strs[j].charAt(i)!= c){
return strs[0].substring(0,i);
}
}
}
return strs[0];
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dangzefei/article/details/124356974
边栏推荐
- 【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
- 华为机试--高精度整数加法
- Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
- Bridge between ischemic stroke and intestinal flora: short chain fatty acids
- Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
- mysql table 中增加列的SQL语句
- Differences among electric drill, electric hammer and electric pick
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- STM32上μC/Shell移植与应用
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
猜你喜欢
AWS eks add cluster user or Iam role
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
zynq平臺交叉編譯器的安裝
[mapping program design] coordinate azimuth calculation artifact (version C)
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
Improving 3D object detection with channel wise transformer
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
随机推荐
Mysql, binlog log query
补充番外14:cmake实践项目笔记(未完待续4/22)
thymeleaf th:value 为null时报错问题
Recursive call -- Enumeration of permutations
IDE idea automatic compilation and configuration of on update action and on frame deactivation
520.检测大写字母
无线充电全国产化电子元件推荐方案
递归调用--排列的穷举
电钻、电锤、电镐的区别
智能电子秤全国产化电子元件推荐方案
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
Introduction to Cortex-M3 register set, assembly language and C language interface
229. Find mode II
mysql table 中增加列的SQL语句
在AWS控制台创建VPC(无图版)
zynq平臺交叉編譯器的安裝
协程与多进程的完美结合
Supplément: annotation
Summary of MySQL de duplication methods
io. Platform. packageRoot; // ignore: deprecated_ Member_ use