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

[LeetCode]-011-Longest Common Prefix的更多相关文章

  1. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  2. [LeetCode] 14. Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  3. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  4. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  5. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  6. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  7. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  8. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  9. LeetCode(54)-Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...

  10. [leetcode]14. Longest Common Prefix 最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

随机推荐

  1. JS拖拽系列--多元素拖拽,面向对象,es6拖拽

    最近不太忙,终于有时间,研究了一下早就想搞定的拖拽系列,先是写了面向过程式的,再做一个面向对象的,再顺便弄弄继承,最后玩一下ES6的class  不觉用了一天多,收获很大.拖拽弄完,想再弄一个拖放. ...

  2. 使用python 实现 微信好友 个性签名 并 制作 词云图

    环境搭建: pip install itchat numpy wordcloud matplotlib jieba 先把上面的几个包安装完成,直接上代码 import itchat from itch ...

  3. localStorage 理解

    localStorage对象是HTML5的客户端存储持久化数据的方案.为了能访问到同一个localStorage对象,页面必须来自同一个域名(子域名无效),使用同一种协议,在同一个端口上. 过期策略: ...

  4. Java的duotaix

    今天看到博客园上一位原创的博文讲解Java多态性,觉得不错,不过没有解释,特此注释,侵删 public class MyTest { public static void main(String ar ...

  5. docker 开启特权模式

    可以在创建容器时通过 --privileged=true 开启特权模式. 创建容器: # docker run -d --name centos7 --privileged=true centos7: ...

  6. Delphi 指令符

  7. install-info - 更新 info/dir 项

    SYNOPSIS 总览 install-info [OPTION]... [INFO-FILE [DIR-FILE]] DESCRIPTION 描述 从 Info 目录文件 DIR-FILE 中的文件 ...

  8. nodejs express 部署

    一.express 4.x版本之前 全局安装express 命令是 npm install express -g express 4.x版本之后 全局安装express 命令是 npm install ...

  9. Inno setup 开源的安装包打包软件

    Inno Setup是一个开源的安装包打包软件,下载地址是:http://www.jrsoftware.org/isdl.php 使用引导界面创建一个安装包打包 配置参考官方文档:http://www ...

  10. Linux磁盘分区与lvm逻辑卷

    硬盘接口的种类分四类:(价格由低到高) IDE SATA硬盘:别名串口硬盘,具有较强的纠错能力. SCSI硬盘:即采用SCSI接口的硬盘,SCSI接口具有应用范围广,多任务,带宽大,CPU占用率低. ...