题目:

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

题解:

解题思路是,先对整个String数组预处理一下,求一个最小长度(最长前缀肯定不能大于最小长度)。

然后以第0个字符串作为参照,从第1个字符串到最后一个字符串,对同一位置做判断,有不同字符串返回当前记录的字符串就行。

我的代码如下,不是那么简洁好看,下面有个整理的更好一些:

 1     private static int MinLength(String[] strs) {
 2         int temp = Integer.MAX_VALUE;
 3         for(int i=0; i<strs.length;i++){
 4             if(temp>strs[i].length())
 5                 temp = strs[i].length();
 6         }
 7         return temp;
 8     }
 9     
     public static String longestCommonPrefix(String[] strs) {
         if(strs.length==0){
             return "";
         }
         int j = 0;
         boolean flag = false;
         
         int length = MinLength(strs);
         
         while(j<length){
             int i = 1;
             while(i<strs.length){
                 int c = strs[0].charAt(j);
                 if(strs[i].charAt(j)==c){
                     i++;
                     }else{
                         flag = true;
                         break;
                     }
                 }
             if(flag)
                 break;
                 j++;
             }
         
         return strs[0].substring(0, j);
         }

更简洁的代码:

 1     private int minlen(String[] strs) {
 2         int min = Integer.MAX_VALUE;
 3         for(int i=0; i<strs.length;i++)
 4             min = Math.min(min,strs[i].length());
 5         return min;
 6     }
 7     
 8     public String longestCommonPrefix(String[] strs) {
 9         if (strs == null || strs.length == 0)
             return "";
         
         StringBuilder res = new StringBuilder();
         int index = 0;
         int len = minlen(strs);
         while (index < len){
             for (int i=1; i<strs.length;i++){
                 if (strs[i].charAt(index) != strs[0].charAt(index))
                     return res.toString();
             }
             res.append(strs[0].charAt(index));
             index++;
         }
         return res.toString();
     }

Reference:http://blog.csdn.net/linhuanmars/article/details/21145733

-------------------

更新

空间复杂度更小的代码如下(from discussion):

 1     public String longestCommonPrefix(String[] strs) {
 2         if(strs.length == 0||strs == null)
 3             return "";
 4             
 5         for(int i = 0; i<strs[0].length(); i++){
 6             char x = strs[0].charAt(i);
 7             for(int j = 1; j<strs.length; j++){
 8                 if(strs[j].length() == i || strs[j].charAt(i) != x)
 9                     return strs[0].substring(0,i);
             }
         }
         
         return strs[0];
     }

Longest Common Prefix leetcode java的更多相关文章

  1. Longest Common Prefix [LeetCode 14]

    1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...

  2. Longest common prefix | leetcode

    Write a function to find the longest common prefix string amongst an array of strings. 思路:要去是寻找字符串ve ...

  3. 「Leetcode」14. Longest Common Prefix(Java)

    分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...

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

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

  5. Java [leetcode 14] Longest Common Prefix

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

  6. LeetCode第[14]题(Java): Longest Common Prefix

    题目:最长公共前缀 难度:EASY 题目内容: Write a function to find the longest common prefix string amongst an array o ...

  7. [LeetCode][Java] Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. 题意: 写出一个函 ...

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

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  9. [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. 2018年全国多校算法寒假训练营练习比赛(第二场)B - TaoTao要吃鸡

    链接:https://www.nowcoder.com/acm/contest/74/B来源:牛客网 题目描述 Taotao的电脑带不动绝地求生,所以taotao只能去玩pc版的荒野行动了, 和绝地求 ...

  2. TCP流嗅探和连接跟踪工具tcpick

    TCP流嗅探和连接跟踪工具tcpick   由于网络通信协议众多,TCP连接状态众多,所以TCP分析较为复杂.Kali Linux提供一款专用工具tcpick.该工具支持在线实时嗅探和离线文件嗅探.它 ...

  3. hihocoder#1046 K个串 可持久化线段树 + 堆

    首先考虑二分,然后发现不可行.... 注意到\(k\)十分小,尝试从这里突破 首先用扫描线来处理出以每个节点为右端点的区间的权值和,用可持久化线段树存下来 在所有的右端点相同的区间中,挑一个权值最大的 ...

  4. hdu 5248 贪心

    题意:

  5. bzoj 3306

    以1号节点为根,弄出DFS序,我们发现,对于一个询问:(rt,u),以rt为根,u节点的子树中的最小点权,我们可以根据rt,u,1这三个节点在同一条路径上的相对关系来把它转化为以1为根的在DFS序上的 ...

  6. python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名

    import MySQLdb #connect try: conn = MySQLdb.connect( host = "localhost", user = "root ...

  7. SGU 403 Game with points

    408. Game with points Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: standa ...

  8. Qt 编译boost

    Qt为4.6.2.Boost为1.63.0. 1.安装qt-sdk-win-opensource-2010.02.1.exe. 2.下载boost_1_63_0并解压,如:解压到E盘根目录下. 3.在 ...

  9. addEventListener 的一些好方法 简单粗暴的说给你

    function show(){ document.addEventListener("click",fn,{ "passive":false, "c ...

  10. CentOS 7 下编译安装lnmp之nginx篇详解

    一.安装环境 宿主机=> win7,虚拟机 centos => 系统版本:CentOS Linux release 7.5.1804 (Core),ip地址 192.168.1.168   ...