题目:

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. javascript中switch的用法注意

    switch中文翻译过来是转换.切换的意思.用在js中,各个条件转换而执行不同代码.

  2. python获取文件

    第一种:使用os.walk: # -*- coding: utf-8 -*- import os def Test1(rootDir): list_dirs = os.walk(rootDir) fo ...

  3. 1011 World Cup Betting (20)(20 point(s))

    problem With the 2010 FIFA World Cup running, football fans the world over were becoming increasingl ...

  4. JAVA内存泄漏解决办法

    JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪.详细说明参考这里 ...

  5. luogu P2439 [SDOI2005]阶梯教室设备利用

    题目链接 luogu P2439 [SDOI2005]阶梯教室设备利用 题解 dp 代码 #include<vector> #include<cstdio> #include& ...

  6. luogu4595 [COCI2011-2012#5] POPLOCAVANJE 后缀自动机

    看着就像后缀自动机.... 然后搜了一下,网上一大把的\(AC\)自动机 嗯...... 不管了,打一个试试 然后就过了\(QAQ\) 我们考虑对于每个点\(i\)求出它往前最长能匹配的子串的长度 可 ...

  7. BZOJ.3991.[SDOI2015]寻宝游戏(思路 set)

    题目链接 从哪个点出发最短路径都是一样的(最后都要回来). 脑补一下,最短路应该是按照DFS的顺序,依次访问.回溯遍历所有点,然后再回到起点. 即按DFS序排序后,Ans=dis(p1,p2)+dis ...

  8. hdu 4462 第37届ACM/ICPC 杭州赛区 J题

    题意:有一块n*n的田,田上有一些点可以放置稻草人,再给出一些稻草人,每个稻草人有其覆盖的距离ri,距离为曼哈顿距离,求要覆盖到所有的格子最少需要放置几个稻草人 由于稻草人数量很少,所以状态压缩枚举, ...

  9. bzoj 2961

    根据“点在圆内”关系,列出点P(x0,y0)在圆C(x,y)内的关系: (x-x0)^2+(y-y0)^2 <= x^2+y^2 化简得: 2*x0*x+2*y0*y >= x0^2+y0 ...

  10. mysql 报错ERROR 1064 (42000),原因使用了mysql保留字 (right syntax to use near 'groups)

    msql 8.0 执行语句: SELECT *  FROM groups; 会报如题的错误, 需加反引号进行修改: SELECT *  FROM `groups`; //////////分割线//// ...