题目:

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. Extjs设置或获取cookie

    设置cookie var myCookie = Ext.util.Cookie.set(‘YourCookieName’,'YourValue’); 读取cookie Ext.util.Cookie. ...

  2. JAVAEE——ssm综合练习:CRM系统(包含ssm整合)

    1 CRM项目外观   1. 开发环境 IDE: Eclipse Mars2 Jdk: 1.7 数据库: MySQL 2. 创建数据库 数据库sql文件位置如下图: 创建crm数据库,执行sql 效果 ...

  3. [leetcode DP]91. Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  4. wpf企业应用之主从结构列表

    主从结构在企业级应用中相当常见,这里结合我的例子谈一下wpf中主从结构列表展示的常用做法,具体效果见 wpf企业级开发中的几种常见业务场景. 首先,Model有两种,主表对应model(假设为mode ...

  5. SMACH专题(一)----安装与初探

    最近使用ROS进行任务(Task)执行,深切体会用传统的方法实现是极其繁杂的.比如人脸录入工作,包含人脸检测,识别,语音提示,运动控制,这些子部分基本都是通过订阅话题的回调函数中处理,之间的切换,如人 ...

  6. mysql关联查询和联合查询

    一.内联方式 1.传统关联查询 "select * from students,transcript where students.sid=transcript.sid and transc ...

  7. Git_撤销修改

    自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txt Git is a distributed version co ...

  8. JS对象与PHP对象的对比

    一.对象的创建与访问 1.JS对象的创建与访问 //方式一(通过内置构造函数创建后添加属性) var obj = new Object(); obj.name = 'Lucy'; //通过点添加属性 ...

  9. sql prompt5安装好了,也破解完成了,然后到SQL里面还是没有提示是为什么?

    勾上这个,祝你成功!

  10. cocos2d-x项目101次相遇:使用触摸事件移动 精灵

    cocos2d-x 101次相遇 / 文件夹  1   安装和环境搭建 -xcode  2   Scenes , Director, Layers, Sprites 3   建立图片菜单  4   在 ...