当前位置:网站首页>Leetcode006 -- find the longest common prefix in the string array
Leetcode006 -- find the longest common prefix in the string array
2022-04-23 04:39:00 【singularityDZF】
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test06 {
/**
* Write a function to find the longest common prefix in the string array .
* If no common prefix exists , Returns an empty string "".
*
* Example 1:
* Input :strs = ["flower","flow","flight"]
* Output :"fl"
*
* Example 2:
* Input :strs = ["dog","racecar","car"]
* Output :""
* explain : Input does not have a common prefix .
*
* Tips :
* 1 <= strs.length <= 200
* 0 <= strs[i].length <= 200
* strs[i] It's only made up of lowercase letters
*
* @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://yzsam.com/2022/04/202204230438035367.html
边栏推荐
- 程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
- leetcode007--判断字符串中的括号是否匹配
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- Migrate from MySQL database to AWS dynamodb
- Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
- [AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
- 用D435i录制自己的数据集运行ORBslam2并构建稠密点云
- 520.检测大写字母
- [AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
- QML进阶(四)-绘制自定义控件
猜你喜欢
win10, mysql-8.0.26-winx64.zip 安装
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
[echart] démarrer avec echart
IDE idea automatic compilation and configuration of on update action and on frame deactivation
The perfect combination of collaborative process and multi process
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
Detailed explanation of life cycle component of jetpack
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
Recommended scheme of national manufactured electronic components
AWS eks add cluster user or Iam role
随机推荐
Installation du compilateur croisé de la plateforme zynq
补充番外14:cmake实践项目笔记(未完待续4/22)
MYSQL50道基础练习题
AWS EKS 部署要点以及控制台与eksctl创建的差异
FAQ of foreign lead and alliance Manager
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
io.Platform.packageRoot; // ignore: deprecated_member_use
那些年我面试过的Android开发岗总结(附面试题+答案解析)
A lifetime of needs, team collaboration can play this way on cloud nailing applet
第四章 --- 了解标准设备文件、过滤器和管道
MYSQL去重方法汇总
智能电子秤全国产化电子元件推荐方案
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
IEEE Transactions on Industrial Informatics(TII)投稿须知
从MySQL数据库迁移到AWS DynamoDB
thymeleaf th:value 为null时报错问题
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
leetcode003--判断一个整数是否为回文数
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
Basic operation of sequence table