当前位置:网站首页>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
边栏推荐
- [AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
- AWS EKS 部署要点以及控制台与eksctl创建的差异
- 做数据可视化应该避免的8个误区
- Create VPC in AWS console (no plate)
- 补:注解(Annotation)
- Migrate from MySQL database to AWS dynamodb
- Redis 命令大全
- 2021数学建模国赛一等奖经验总结与分享
- 优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
- MySQL queries users logged in for at least N consecutive days
猜你喜欢

IDE idea automatic compilation and configuration of on update action and on frame deactivation
![[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022](/img/eb/916a3fc1a89356fd8e74b943172ac3.png)
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
![[echart] démarrer avec echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] démarrer avec echart

兼容NSR20F30NXT5G的小体积肖特基二极管

Use recyclerview to realize left-right side-by-side classification selection

Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)

Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)

Record your own dataset with d435i, run orbslam2 and build a dense point cloud

Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library

Brushless motor drive scheme based on Infineon MCU GTM module
随机推荐
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
Supplément: annotation
Create VPC in AWS console (no plate)
leetcode005--原地删除数组中的重复元素
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
zynq平台交叉编译器的安装
Differences among electric drill, electric hammer and electric pick
针对NFT的网络钓鱼
Summary of MySQL de duplication methods
MYSQL去重方法汇总
IDE idea automatic compilation and configuration of on update action and on frame deactivation
QML进阶(四)-绘制自定义控件
电钻、电锤、电镐的区别
C语言:恶搞小游戏
VHDL implementation of 32-bit binary to BCD code
Go reflection rule
2020 is coming to an end, special and unforgettable.
io.Platform.packageRoot; // ignore: deprecated_member_use
Coinbase: basic knowledge, facts and statistics about cross chain bridge
补充番外14:cmake实践项目笔记(未完待续4/22)