当前位置:网站首页>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
边栏推荐
- 单片机串口数据处理(2)——uCOSIII+循环队列接收数据
- 【论文阅读】【3d目标检测】point transformer
- 在AWS控制台创建VPC(无图版)
- 520.检测大写字母
- Detailed explanation of life cycle component of jetpack
- A heavy sword without a blade is a great skill
- IDE idea automatic compilation and configuration of on update action and on frame deactivation
- Eksctl deploying AWS eks
- Basic operation of sequence table
- Summary of MySQL de duplication methods
猜你喜欢
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
Key points of AWS eks deployment and differences between console and eksctl creation
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
win10, mysql-8.0.26-winx64. Zip installation
指纹Key全国产化电子元件推荐方案
MYSQL50道基础练习题
Why recommend you to study embedded
Installation of zynq platform cross compiler
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
随机推荐
win10, mysql-8.0.26-winx64.zip 安装
win10, mysql-8.0.26-winx64. Zip installation
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
Set classic topics
[pytoch foundation] torch Split() usage
Bacterial infection and antibiotic use
单片机串口数据处理(1)——串口中断发送数据
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
Improving 3D object detection with channel wise transformer
Opencv -- yoact case segmentation model reasoning
Coinbase: basic knowledge, facts and statistics about cross chain bridge
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
[echart] Introduction to echart
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
Leetcode->1 两数之和
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
重剑无锋,大巧不工
MySQL queries users logged in for at least N consecutive days
What is the thirty-six plan