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. golang:使用timingwheel进行大量ticker的优化

    Ticker 最近的项目用go实现的服务器需要挂载大量的socket连接.如何判断连接是否还存活就是我们需要考虑的一个问题了. 通常情况下面,socket如果被客户端正常close,服务器是能检测到的 ...

  2. Android学习笔记:对Android应用进行单元测试

     第一步:在AndroidManifest.xml中加入如下两段代码: <manifest xmlns:android="http://schemas.android.com/ap ...

  3. JAVA之旅(八)——多态的体现,前提,好处,应用,转型,instanceof,多态中成员变量的特点,多态的案例

    JAVA之旅(八)--多态的体现,前提,好处,应用,转型,instanceof,多态中成员变量的特点,多态的案例 学习是不能停止的 一.多态 我们今天又要学习一个新的概念了,就是多态,它是面向对象的第 ...

  4. C++ 仿函数/函数指针/闭包lambda

    在上一篇文章中介绍了C++11新引入的lambda表达式(C++支持闭包的实现),现在我们看一下lambda的出现对于我们编程习惯的影响,毕竟,C++11历经10年磨砺,出140新feature,对于 ...

  5. Dynamics CRM 系统自定义部分的语言翻译

    Dynamics CRM 自带语言切换功能,在官网下载所需语言包安装后,在设置语言中就能看到你所添加的语言,勾选要启用的语言应用即可,再打开系统设置--语言就能看到可更改用户界面语言的显示了. 但官方 ...

  6. JAVA之旅(四)——面向对象思想,成员/局部变量,匿名对象,封装 , private,构造方法,构造代码块

    JAVA之旅(四)--面向对象思想,成员/局部变量,匿名对象,封装 , private,构造方法,构造代码块 加油吧,节奏得快点了 1.概述 上篇幅也是讲了这点,这篇幅就着重的讲一下思想和案例 就拿买 ...

  7. Gibbs sampling

    In statistics and in statistical physics, Gibbs sampling or a Gibbs sampler is aMarkov chain Monte C ...

  8. C语言之归并排序

    即将两个都升序(或降序)排列的数据序列合并成一个仍按原序排列的序列. 上代码: #include <stdio.h> #include <stdlib.h> #define m ...

  9. Gradle 1.12用户指南翻译——第三十九章. IDEA 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  10. hbase mlockall

    mlockall 头文件:#include <sys/mman.h> 函数原型:int mlockall(int flags); flags:       MCL_CURRENT --Lo ...