/**
* 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. Python开发——【循环】语句

    while循环 while 条件: # 要执行的循环体 # 如果条件为真,那么循环体则执行 # 如果条件为假,那么循环体不执行 死循环 count = 0 while True:# 条件永远为真 pr ...

  2. 20172325 2018-2019-2 《Java程序设计》第七周学习总结

    20172325 2018-2019-2 <Java程序设计>第七周学习总结 教材学习内容总结 二叉查找树 二叉查找树:是含附加属性的二叉树,即其左孩子小于父节点,而父节点又小于或等于右孩 ...

  3. bittorrent 学习(一) 种子文件分析与bitmap位图

    终于抽出时间来进行 BITTORRENT的学习了 BT想必大家都很熟悉了,是一种文件分发协议.每个下载者在下载的同时也在向其他下载者分享文件. 相对于FTP HTTP协议,BT并不是从某一个或者几个指 ...

  4. python爬虫笔记

    1.抓取网页并保存到txt中.解决控制台乱码问题 #_*_coding:utf-8_*_ import urllib2 response = urllib2.urlopen('http://hws.m ...

  5. 【APP测试(Android)】--交叉事件

  6. 【轻松前端之旅】CSS盒子模型

    盒子模型,也叫框模型,在CSS里是很重要的概念. 每个元素都可以看做一个盒子.盒子包含四个部分:外边距(margin).边框(border).内边距(padding).元素内容(element con ...

  7. Nodejs之路:非I/O的异步API

    本篇主要介绍setTimeout,setInterval,setImmediate和process.nextTick. 1,定时器 Node中的定时器和浏览器中用法一致.区别在于:在Node中,执行到 ...

  8. using python read/write HBase data

    A. operations on Server side 1. ensure hadoop and hbase are working properly 2. install thrift:  apt ...

  9. 三.mysql表的完整性约束

    mysql表的完整性约束 什么是约束 not null    不能为空的 unique      唯一 = 不能重复 primary key 主键 = 不能为空 且 不能重复 foreign key ...

  10. verilog HDL -模块代码基本结构

    1--verilog HDL 语言的预编译指令作用:指示在编译verliog HDL源代码前,需要执行哪些操作. 2--模块内容是嵌在module 和endmodule两个语句之间.每个模块实现特定的 ...