lintcode -- 最长公共前缀
来源:互联网 发布:苹果手机 解压缩软件 编辑:程序博客网 时间:2024/05/21 18:35
给k个字符串,求出他们的最长公共前缀(LCP)
在 "ABCD" "ABEF" 和 "ACEF" 中, LCP 为 "A"
在 "ABCDEFG", "ABCEFG", "ABCEFA" 中, LCP 为 "ABC"
/*
思路:
1.先获取第一个字符串
2.循环把第一个数和后一个数比较,得到共同字串,
3.再把字串和后一个数拿来比较
*/
public class Solution {
public String longestCommonPrefix(String[] strs) {
// write your code here
if(strs == null || strs.length ==0)return "";
String com = strs[0];
for(int i=1;i<strs.length;i++){
//计数
int index =0;
while(index<strs[i].length()&&index<com.length()
&&strs[i].charAt(index) == com.charAt(index)){
//!!不能用if比较strs[i].charAt(index) == com.charAt(index)
index++;
}
if(index==0)return "";
////再求出共同的字串,再去和之后的对比
com = com.substring(0,index);
}
return com;
}
}
- LintCode-最长公共前缀
- LintCode 最长公共前缀
- lintcode ---- 最长公共前缀
- LintCode : 最长公共前缀
- lintcode,最长公共前缀
- 最长公共前缀 -LintCode
- [Lintcode]最长公共前缀
- lintcode -- 最长公共前缀
- lintcode 最长公共前缀
- Lintcode(6)-最长公共前缀
- lintcode-最长公共前缀-78
- lintcode之最长公共前缀
- LintCode(M)最长公共前缀
- LintCode 78.最长公共前缀
- LintCode 78 最长公共前缀
- [Lintcode] #78 最长公共前缀
- [LintCode 78] 最长公共前缀(Python)
- lintcode python ——最长公共前缀(LCP)
- python theano中的shared variable
- Java web开发加载图片路径的两种方式
- 日期计算
- 【Mail】Tomcat提供JNDI方式支持JavaMail(三)
- 设计模式之简单工厂模式,工厂方法模式,抽象工厂模式
- lintcode -- 最长公共前缀
- Git常用命令
- HDU 1263 水果 map
- 思考感悟2
- n进制数q转换成m进制数
- linux自带i2c工具使用
- codeforces 214B Hometask
- PAT1045快速排序
- bzoj2946 [Poi2000]公共串 后缀自动机