题目:

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

题解:

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

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

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

  1.  1     private static int MinLength(String[] strs) {
  2.  2         int temp = Integer.MAX_VALUE;
  3.  3         for(int i=0; i<strs.length;i++){
  4.  4             if(temp>strs[i].length())
  5.  5                 temp = strs[i].length();
  6.  6         }
  7.  7         return temp;
  8.  8     }
  9.  9     
  10.      public static String longestCommonPrefix(String[] strs) {
  11.          if(strs.length==0){
  12.              return "";
  13.          }
  14.          int j = 0;
  15.          boolean flag = false;
  16.          
  17.          int length = MinLength(strs);
  18.          
  19.          while(j<length){
  20.              int i = 1;
  21.              while(i<strs.length){
  22.                  int c = strs[0].charAt(j);
  23.                  if(strs[i].charAt(j)==c){
  24.                      i++;
  25.                      }else{
  26.                          flag = true;
  27.                          break;
  28.                      }
  29.                  }
  30.              if(flag)
  31.                  break;
  32.                  j++;
  33.              }
  34.          
  35.          return strs[0].substring(0, j);
  36.          }

更简洁的代码:

  1.  1     private int minlen(String[] strs) {
  2.  2         int min = Integer.MAX_VALUE;
  3.  3         for(int i=0; i<strs.length;i++)
  4.  4             min = Math.min(min,strs[i].length());
  5.  5         return min;
  6.  6     }
  7.  7     
  8.  8     public String longestCommonPrefix(String[] strs) {
  9.  9         if (strs == null || strs.length == 0)
  10.              return "";
  11.          
  12.          StringBuilder res = new StringBuilder();
  13.          int index = 0;
  14.          int len = minlen(strs);
  15.          while (index < len){
  16.              for (int i=1; i<strs.length;i++){
  17.                  if (strs[i].charAt(index) != strs[0].charAt(index))
  18.                      return res.toString();
  19.              }
  20.              res.append(strs[0].charAt(index));
  21.              index++;
  22.          }
  23.          return res.toString();
  24.      }

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

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

更新

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

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

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. Oceanus:美团HTTP流量定制化路由的实践

    背景简述 Oceanus是美团基础架构部研发的统一HTTP服务治理框架,基于Nginx和ngx_lua扩展,主要提供服务注册与发现.动态负载均衡.可视化管理.定制化路由.安全反扒.session ID ...

  2. python opencv3 获取摄像头视频

    git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 捕获摄像头10 ...

  3. win 10 文件夹 背景 没效果

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha  313134555@qq.com

  4. Java基础学习——多线程之线程池

    1.线程池介绍     线程池是一种线程使用模式.线程由于具有空闲(eg:等待返回值)和繁忙这种不同状态,当数量过多时其创建.销毁.调度等都会带来开销.线程池维护了多个线程,当分配可并发执行的任务时, ...

  5. Android之基于HTTP协议的通信详解

    Android系统中本身是有下载机制的,比如浏览器使用的DownloadManager.可遗憾的是,DownloadManager只提供给浏览器使用,一般的应用程序没法调用它. 另外,如果下载调用频繁 ...

  6. service redis does not support chkconfig 的解决办法

    问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中(加入以下注释): # chkconfig: # description: Redis is a persi ...

  7. spring---aop(10)---Spring AOP中AspectJ

    写在前面 在之前的文章中有写到,Spring在配置中,会存在大量的切面配置.然而在很多情况下,SpringAOP 所提供的切面类真的不是很够用,比如想拦截制定的注解方法,我们就必须扩展DefalutP ...

  8. MyBatis连接SQL Server的关键点

    一.Maven包配置 <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> < ...

  9. Linux线程 之 线程 线程组 进程 轻量级进程(LWP) -systemtap -mysql

    http://blog.chinaunix.net/uid-24774106-id-3650136.html http://blog.itpub.net/15480802/viewspace-7627 ...

  10. blkblock工具1

    http://www.ibm.com/developerworks/cn/linux/l-cn-perf1/ http://blog.chinaunix.net/uid-24774106-id-409 ...