当前位置:网站首页>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
边栏推荐
- eksctl 部署AWS EKS
- Nature medicine reveals individual risk factors of coronary artery disease
- Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
- Inverse system of RC low pass filter
- leetcode004--罗马数字转整数
- 从MySQL数据库迁移到AWS DynamoDB
- IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
- 数据孤岛是什么?为什么2022年仍然存在数据孤岛?
- AWS eks add cluster user or Iam role
- 2019 is coming to an end, the longest day.
猜你喜欢
229. 求众数 II
Key points of AWS eks deployment and differences between console and eksctl creation
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
智能电子秤全国产化电子元件推荐方案
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
Interaction of diet gut microbiota on cardiovascular disease
STM32 MCU ADC rule group multi-channel conversion DMA mode
协程与多进程的完美结合
Improving 3D object detection with channel wise transformer
Recommended scheme of national manufactured electronic components
随机推荐
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
A heavy sword without a blade is a great skill
无线键盘全国产化电子元件推荐方案
Mysql50 basic exercises
补充番外14:cmake实践项目笔记(未完待续4/22)
leetcode002--将有符号整数的数字部分反转
Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
IDE idea automatic compilation and configuration of on update action and on frame deactivation
win10, mysql-8.0.26-winx64.zip 安装
QML advanced (IV) - drawing custom controls
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
Phishing for NFT
補:注解(Annotation)
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
C语言:恶搞小游戏
383. 赎金信
Go反射法则
Summary of MySQL de duplication methods
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
从MySQL数据库迁移到AWS DynamoDB