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. 洛谷 P2015 二叉苹果树 题解

    题面 裸的树上背包: 设f[u][i]表示在以u为子树的树种选择i条边的最大值,则:f[u][i]=max(f[u][i],f[u][i-j-1]+f[v][k]+u到v的边权); #include ...

  2. Centos7安装Beanstalkd

    安装 //安装 yum -y install beanstalkd --enablerepo=epel //查看版本 beanstalkd -v //启动 -b断电重启会恢复 /usr/bin/bea ...

  3. 深入理解let和var的区别

    首先我们应该知道js引擎在读取js代码时会进行两个步骤: 第一个步骤是解释. 第二个步骤是执行. 所谓解释就是会先通篇扫描所有的Js代码,然后把所有声明提升到顶端,第二步是执行,执行就是操作一类的. ...

  4. jstl用法 简介

    <c:choose> <c:when test="${salary <= 0}"> 太惨了. </c:when> <c:when t ...

  5. jQuery改变元素class属性

    //去掉class属性 $(this).parent('li').removeClass("prev_selected"); //去掉同兄弟的class属性. $(this).pa ...

  6. 十三、LaTex中的参考文献BibTex

    将默认文献工具设置为bibtex

  7. iPhone屏幕适配,历史及现状(http://hjcapple.github.io/2014/10/10/iphone-screen.html)

    iPhone屏幕适配,历史及现状 初代iPhone 2007年,初代iPhone发布,屏幕的宽高是320×480像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone 3GS的也保持不变 ...

  8. MathType 7.4.2.480

    目录 1. 相关推荐 2. 按 3. 软件介绍 4. 安装步骤 5. 使用说明 6. 下载地址 1. 相关推荐 推荐使用:AxMath(AxMath可以与LaTeX进行交互):https://blog ...

  9. AIX中设备管理

    1.AIX系统中的设备概述 逻辑设备文件     #ls   -l  /dev   空设备文件 #/dev/null   设备的状态:undefined.defined.available.stopp ...

  10. 【转】Android编译系统详解(一)——build/envsetup.sh

    出处 http://www.cloudchou.com/android/post-134.html 本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 准备好编译环境后,编译Rom的 ...