题目:

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. Eclipse 更改默认的编码 和 换行符

  2. 你见过这些JavaScript的陷阱吗?

    一.成员操作符 Number.prototype.testFn=function () { console.log(this<0, this.valueOf()); } var num = -1 ...

  3. leetcode 算法 Excel表列序号 python实现

    这道题给我感觉就像一个26进制数一样. A 就是1 B是2 .... Z 是26 如果AB 两位,那就是  1 * 26 + 2   就是A 的数值*26 + B的数值 如果是MNP 三位数   那就 ...

  4. C# 集合类-使用

    关联性集合类 关联性集合类即我们常说的键值对集合,允许我们通过Key来访问和维护集合. 我们来看一下  .net 为我们提供了哪些泛型的关联性集合类: Dictionary<TKey,TValu ...

  5. WinForm 数据库无限填充树目录 treeView

    我自己想的是处理数据库每一条数据,然后来插入子节点的子节点. 奈何没有插入子节点的子节点的办法,百度来百度去,一看全都是递归. 本来我是绝望的, 但是没办法,老板的需求不能驳回啊,于是就来ctrl c ...

  6. BZOJ 1283 序列 费用流 网络流 线性规划

    https://darkbzoj.cf/problem/1283 给出一个长度为N的正整数序列Ci,求一个子序列,使得原序列中任意长度为M的子串中被选出的元素不超过K(K,M<=100) 个,并 ...

  7. python开发_tkinter_复选菜单

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  8. SnackDown Longest Increasing Subsequences 构造题

    Longest Increasing Subsequences 题目连接: https://www.codechef.com/SNCKPA16/problems/MAKELIS Description ...

  9. C#高级编程9 第11章 Linq

    Linq 1.Linq概述 列表和实体 准备数据: public class Championship { public int Year { get; set; } public string Fi ...

  10. centos7 docker安装和使用_入门教程

    说明:本文也是参考互联网上的文章写的,感谢相关作者的贡献. 操作系统 64位CentOS Linux release 7.2.1511 (Core) 配置好IP:192.168.1.160 修改yum ...