当前位置:网站首页>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
边栏推荐
- 2019 is coming to an end, the longest day.
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- Coinbase:关于跨链桥的基础知识、事实和统计数据
- zynq平臺交叉編譯器的安裝
- Bacterial infection and antibiotic use
- Mysql, binlog log query
- 单片机串口数据处理(2)——uCOSIII+循环队列接收数据
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- 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)
猜你喜欢
随机推荐
STM32单片机ADC规则组多通道转换-DMA模式
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
VHDL implementation of 32-bit binary to BCD code
智能电子秤全国产化电子元件推荐方案
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
Eksctl deploying AWS eks
RC低通滤波器的逆系统
Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
在AWS控制台创建VPC(无图版)
Create VPC in AWS console (no plate)
[mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
Go反射—Go语言圣经学习笔记
Supplément: annotation
test
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
Coinbase:关于跨链桥的基础知识、事实和统计数据
TreeSet after class exercises
Leetcode->1 两数之和
Detailed explanation of life cycle component of jetpack








