Description:

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

Thoughts:

1.定义一个结果字符串result="";

2.如果List的长度为0,那么直接返回result;

3.找到数组中最短的字符串min_str;

4.将min_str从索引为0开始的字符,逐一的和其他的字符串的相应位置的字符进行比较

5.如果所有字符串当前的字符都是一致的话,就将当前字符append到result中;否则的话返回result;

6.重复过程4,5直到min_str全部比较完成

以下是我的java代码

package easy;

public class LongestCommonPrefix {
/*返回长度最短的字符串在List中所在的位置*/
private static int shortestStringLength(String[] strs){ //判断当前的List中是否有字符串,没有的话返回-1,否则返回当前list中长度最短的字符串所在的位置
if(strs.length == 0){
return -1;
}else{
int result = strs[0].length();
int record = 0;
for(int i = 1; i<strs.length;i++){
if(result>strs[i].length()){
result = strs[i].length();
record = i;
}
}
return record;
} } public static String longestCommonPrefix(String[] strs){
String result = "";
int m = shortestStringLength(strs);
/*shortestStringLength的返回值等于-1,说明strs中没有字符串,结果为空;
* 否则的话,利用最短的字符串,逐个的比较其他的字符串是否包含当前最短字符串的前缀。从而不断地增加前缀的长度。
*/
if(m == -1){
return result;
}else{
int n = strs.length;
for(int i = 0;i<strs[m].length();i++){
char a = strs[m].charAt(i);
for(int j = 0; j< n;j++){
if(a != strs[j].charAt(i)){
return result;
}
}
result = result+a;
}
return result;
} } public static void main(String[] args){
String[] strs = new String[]{"ac","ac","a","a"};
String result = longestCommonPrefix(strs);
System.out.println(result);
}
}

longestCommonPrefix的更多相关文章

  1. leetcode — longest-common-prefix

    /** * Source : https://oj.leetcode.com/problems/longest-common-prefix/ * * Created by lverpeng on 20 ...

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

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

  3. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  4. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  5. 全部leetcode题目解答(不含带锁)

    (记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.)   88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...

  6. python leetcode 1

    开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...

  7. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  8. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  9. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

随机推荐

  1. MySQL慢查询优化 EXPLAIN详解

            我们平台过一段时间就会把生产数据库的慢查询导出来分析,要嘛修改写法,要嘛新增索引.以下是一些笔记.总结整理 慢查询排查         show status;  // 查询mysql ...

  2. Chapter 2 User Authentication, Authorization, and Security(3):保护服务器避免暴力攻击

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38756693,专题目录:http://blog.csdn.net/dba_huangzj ...

  3. 【翻译】Ext JS 6 Beta发布

    原文:Ext JS 6 Beta is Now Available 概述 Ext JS 6的好处 新的Ext JS功能和工具 需要你的反馈意见 概述 很高兴,Ext JS 6 beta版本现在发布了. ...

  4. Cocos2D v3.x中关于重叠触摸层优先级的问题

    在Cocos2D v2.x版本中可以通过以下方法设置本层的触摸优先级: [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate ...

  5. android smartbar适配

    1.使用魅族的demo里的SmartBarUtils.java 2.在mainifest中的Application         android:theme="@android:style ...

  6. 认识 SurfaceView

    SurfaceView是基于View视图进行扩展的视图类,适用于2D游戏开发,主要特点有: [1]surfaceView中对于画布的重绘是由一个新的线程去绘制,因此可以处理一些耗时的操作 [2]sur ...

  7. hive语句嵌入python脚本(进行map和reduce,实现左外连接)

    在Hive语句中使用脚本(如python和shell)进行map和reduce:利用命令transform(或者指定map和reduce),配合加入的脚本文件add file 请看:http://ww ...

  8. Mahout系列之----共轭梯度预处理

    对于大型矩阵,预处理是很重要的.常用的预处理方法有: (1) 雅克比预处理 (2)块状雅克比预处理 (3)半LU 分解 (4)超松弛法

  9. RHEL6从源码安装python及其他软件包

    RHEL6从源码安装python及其他软件包 ## install ssl $ sudo yum install openssl-devel or: $ sudo apt-get install li ...

  10. Smarty学习笔记(一)

    1.Smarty的配置: 将lib的内容复制到自己的工程,然后引入 实例化和配置Smarty基本属性: $smarty = new Smarty(); $smarty->left_delimit ...