当前位置:网站首页>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
边栏推荐
- 基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
- 383. 赎金信
- 【论文阅读】【3d目标检测】point transformer
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- Chlamydia infection -- causes, symptoms, treatment and Prevention
- A lifetime of needs, team collaboration can play this way on cloud nailing applet
- eksctl 部署AWS EKS
- 重剑无锋,大巧不工
- leetcode009--用二分查找在数组中搜索目标值
- HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
猜你喜欢
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
Iron and intestinal flora
MYSQL去重方法汇总
2021数学建模国赛一等奖经验总结与分享
Opencv -- yoact case segmentation model reasoning
补充番外14:cmake实践项目笔记(未完待续4/22)
Use recyclerview to realize left-right side-by-side classification selection
MYSQL50道基础练习题
Why recommend you to study embedded
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
随机推荐
Redis command Encyclopedia
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
Introduction to Cortex-M3 register set, assembly language and C language interface
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
VHDL implementation of 32-bit binary to BCD code
Supplement: Annotation
Coinbase:关于跨链桥的基础知识、事实和统计数据
mysql table 中增加列的SQL语句
Recommended scheme of national manufactured electronic components
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
383. Ransom letter
STM32 upper μ C / shell transplantation and Application
Differences among electric drill, electric hammer and electric pick
那些年我面试过的Android开发岗总结(附面试题+答案解析)
Matlab minimalist configuration of vscode configuration
Create VPC in AWS console (no plate)
Mysql---数据读写分离、多实例
test
AWS EKS添加集群用户或IAM角色
洛谷P1858 【多人背包】 (背包求前k优解)