[LeetCode]-011-Longest Common Prefix
2024-08-25 00:01:04
Write a function to find the longest common prefix string amongst an array of strings.
[]
=>""
["abcweed","htgdabc","sabcrf"]
=>""
["abcweed","abhtgdc","abacrf"]
=>"ab"
题目大意:求字符串数组的最长子前缀
public class Solution{
public String longestCommonPrefix(String[] strs) {
//System.out.println(Arrays.toString(strs));
if(strs.length == 0)
return "";
int min = Integer.MAX_VALUE;
for(String s : strs){
if(min>s.length())
min = s.length();
}
if(min==0)
return "";
//System.out.println(min);
String prefix = "", tmp = "";
int i=0;
for( ; i<min; i++){
prefix = strs[0].substring(0,(i+1));
//System.out.println("prefix=" + prefix);
for(int j=1; j<strs.length; j++){
tmp = strs[j].substring(0,(i+1));
//System.out.println("tmp=" + tmp);
if(!prefix.equals(tmp))
return strs[0].substring(0,(i));
}
}
System.out.println("i=" + strs[0].substring(0,(i)));
return "";
} public static void main(String[] args){
String[] arr = {"","asdffg","aswd"};
if(args.length!=0)
arr = args;
Solution solution = new Solution();
String res = solution.longestCommonPrefix(arr);
System.out.println(res);
}
}
最新文章
- Elasticsearch查询——布尔查询Bool Query
- iOS第三方Api及常用框架总结
- 遍历map的常用方法
- nginx专题
- hdu 1175冒牌连连看
- 3、IOS开发--iPad之仿制QQ空间 (为HomeViewController添加交互逻辑 并 为导航条内容添加UISegmentedControl)
- 配置错误 在唯一密钥属性“fileExtension”设置为“.log”时,无法添加类型为“mimeMap”的重复集合项
- Java问题汇集(2)
- php根据经纬度计算距离和方向--摘录自http://haotushu.sinaapp.com/post-520.html
- 刚安装的ios app 会带有教你功能使用的特效说明 做法
- html 基础之 <;link>;标签
- contextServlet
- android 缓存实现
- Redis 高级部分
- php curl cookie 读写
- Android 代码混淆配置总结
- Pycharm 连接Linux 远程开发
- WPF 后台添加DataGrid
- chrome浏览器插件开发经验(一)
- Android-自己定义PopupWindow