当前位置:网站首页>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
边栏推荐
- 【Echart】echart 入門
- IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
- QML advanced (IV) - drawing custom controls
- 229. 求众数 II
- Set classic topics
- Understand the gut organ axis, good gut and good health
- 补充番外14:cmake实践项目笔记(未完待续4/22)
- Leetcode->1 两数之和
- A lifetime of needs, team collaboration can play this way on cloud nailing applet
- 补:注解(Annotation)
猜你喜欢
![[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022](/img/48/0e95841743bada4faf3edfee31cb6a.png)
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022

Use recyclerview to realize left-right side-by-side classification selection
![[echart] démarrer avec echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] démarrer avec echart

那些年我面试过的Android开发岗总结(附面试题+答案解析)

AWS eks add cluster user or Iam role

Bacterial infection and antibiotic use

Interaction of diet gut microbiota on cardiovascular disease

无线键盘全国产化电子元件推荐方案

IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置

Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
随机推荐
Chlamydia infection -- causes, symptoms, treatment and Prevention
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
Huawei machine test -- high precision integer addition
华为机试--高精度整数加法
Supplément: annotation
[mapping program design] coordinate azimuth calculation artifact (version C)
Installation du compilateur croisé de la plateforme zynq
【Pytorch基础】torch.split()用法
A new method for evaluating the quality of metagenome assembly - magista
thymeleaf th:value 为null时报错问题
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
[pytoch foundation] torch Split() usage
Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
电钻、电锤、电镐的区别
C语言常用字符串处理函数
Installation and use of Apache bench (AB pressure test tool)
STM32上μC/Shell移植与应用
Iron and intestinal flora
Supplement: Annotation
A heavy sword without a blade is a great skill