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);
}
}

最新文章

  1. Elasticsearch查询——布尔查询Bool Query
  2. iOS第三方Api及常用框架总结
  3. 遍历map的常用方法
  4. nginx专题
  5. hdu 1175冒牌连连看
  6. 3、IOS开发--iPad之仿制QQ空间 (为HomeViewController添加交互逻辑 并 为导航条内容添加UISegmentedControl)
  7. 配置错误 在唯一密钥属性“fileExtension”设置为“.log”时,无法添加类型为“mimeMap”的重复集合项
  8. Java问题汇集(2)
  9. php根据经纬度计算距离和方向--摘录自http://haotushu.sinaapp.com/post-520.html
  10. 刚安装的ios app 会带有教你功能使用的特效说明 做法
  11. html 基础之 &lt;link&gt;标签
  12. contextServlet
  13. android 缓存实现
  14. Redis 高级部分
  15. php curl cookie 读写
  16. Android 代码混淆配置总结
  17. Pycharm 连接Linux 远程开发
  18. WPF 后台添加DataGrid
  19. chrome浏览器插件开发经验(一)
  20. Android-自己定义PopupWindow

热门文章

  1. 单节点FastDFS安装
  2. python一行代码打印Love心形
  3. day 14 装饰器
  4. c# 动态加载和卸载DLL程序集
  5. 苹果浏览器和ios中,时间字符串转换问题
  6. 启动VMware出现报错:The VMware Authorization Service is not running
  7. 2&gt;&amp;1的含义解释
  8. SpringBoot01——Framework Introduced and Helloworld
  9. python打开文件的方式
  10. crontab误删操作的恢复与防范