/**
* Source : https://oj.leetcode.com/problems/longest-common-prefix/
*
* Created by lverpeng on 2017/7/10.
*
* Write a function to find the longest common prefix string amongst an array of strings.
*/
public class LongestCommonPrefix { /**
* 依次比较每个字符串的每个字符是否相同
*
*
* @param strArr
* @return
*/
public String findLongestPrefix (String[] strArr) {
String prefix = "";
for (int i = 0; i < strArr[0].length(); i++) {
boolean equal = true;
for (int j = 0; j < strArr.length; j++) {
if (i >= strArr[j].length()) {
equal = false;
break;
}
if (j == 0) {
continue;
}
if (strArr[j].charAt(i) != strArr[j - 1].charAt(i)) {
equal = false;
}
}
if (!equal) {
break;
} else {
prefix += strArr[0].charAt(i);
}
}
return prefix;
} public static void main(String[] args) {
LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
String[] strArr = new String[]{"abc", "a", "abcd"};
System.out.println("a-------" + longestCommonPrefix.findLongestPrefix(strArr)); strArr = new String[]{"abcsdfg", "abc", "abcdasdf"};
System.out.println("abc-------" + longestCommonPrefix.findLongestPrefix(strArr));
}
}

leetcode — longest-common-prefix的更多相关文章

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

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

  2. Leetcode Longest Common Prefix

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

  3. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

  4. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  5. [LeetCode] Longest Common Prefix 字符串公有前序

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

  6. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  7. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  8. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  9. leetcode Longest Common Prefix 多个字符串的最长字串

    public class Solution { public String get(String a,String b) { if(a==""||b=="") ...

  10. leetcode Longest Common Prefix python

    class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...

随机推荐

  1. MySQL开发——【数据库、数据表的基本操作】

    启动MySQL服务器端 CMD启动MySQL服务器端 net start(启动)|stop(停止)|restart(重启)服务名称(mysql) 连接MySQL服务器端 CMD连接MySQL服务器端 ...

  2. 快速创建IIS站点并设置权限

     net user WebSiteUser WebSiteUserWebSiteUser /add /yWMIC Path Win32_UserAccount Where Name="Web ...

  3. redis持久化详述

    本来打算根据自己搜索的一些文章写些总结,后来发现了一篇好文,这里转载下,在自己博客里面记录下. 原文链接:https://www.cnblogs.com/kismetv/p/9137897.html ...

  4. idea执行mapreduce报错 Could not locate Hadoop executable: C:\hadoop-3.1.1\bin\winutils.exe

    window执行mapreduce报错 Exception in thread "main" java.lang.RuntimeException: java.io.FileNot ...

  5. 中国移动物联网ONENET平台数据本地采集工具

    吧从中国移动物联网平台上接收的数据 实时按天保存为CSV文件或者是SQL SERVER数据库中方便进行数据处理 还可设置显示最大值,最小值,报警值,报警推送,tts语音报警等贴心功能

  6. httpd-vhosts.conf配置

    <VirtualHost 127.0.0.1:80> ServerName zjm.appems.com DocumentRoot E:\qian\web <Directory &q ...

  7. CentOS添加明细路由

    网络架构图   根据最近为客户设计的网络架构,简单的梳理一个网路架构图,当然实际上的网络架构要比这个架构图复杂的多,咱们这边只是用这个图作为一个简单的示例. 拓扑分析   我们要实现专线两端不同网段的 ...

  8. rand_1tom 产生 rand_1ton

    给定一个等概率随机产生1~M的随机函数rand1ToM如下: public int rand1ToM(int m) { return (int) (Math.random() * m) + 1; } ...

  9. JSLint在idea编译器中报错

    jslint:this function needs a 'use strict' pragma报错解决 JSInt是指在编写代码的时候进行代码检查语法,没有必要开启 关闭的方法 在setting中搜 ...

  10. MySQL-Cluster 和主从(Master,Slave)搭建总结

    双主互备,主从 什么是双主 MultiSource 多源复制 原理及流程图 主要步骤 1,在 Master Server 上开启 bin log 日志 和 设置 server-id  :(在my.cn ...