package javaLeetCode.primary;

/**
* Write a function to find the longest common prefix string amongst an array of strings.
* If there is no common prefix, return an empty string "".
* */
public class LongestCommonPrefix_14 {
public static void main(String[] args) {
String[] strs = new String[]{"flower","flow","flight"};
System.out.println(longestCommonPrefix_1(strs));
}//end main() /**
* Make full use of the methods of the String class.<br>
* 1. Find the shortest string.<br>
* 2. Compare it to each element of the string array from beginning to end.<br>
* 3. If it does not match any of the elements in the string array, it is clipped off the last character and out of the for loop.<br>
* 4. When its length is zero, break out of the while loop.
* */ /*
* Test Data:
* ["dog","racecar","car"]
* ["flower","flow","flight"]
* ["ca","a"]
* ["a"]
* ["aca","cba"]
* */
public static String longestCommonPrefix_1(String[] strs) { if (strs == null || strs.length == 0)
return "";
// Find the shortest string.
String minString = strs[0];
for(int i=1;i<strs.length;i++) {
if(minString.length()>strs[i].length()) {
minString = strs[i];
}else {
continue;
}//end if
}//end for //Find whether other strings include the minString.
boolean isInclude = true;
while(minString!=null) {
for(int i=0;i<strs.length;i++) {
if(strs[i].startsWith(minString)) {
isInclude = true;
}else {
minString=minString.substring(0, minString.length()-1);
isInclude = false;
break;
}//end if
}//end for
if(isInclude == true)
break;
else ;
}//end while return minString; }//end longestCommonPrefix() /**
* Use the simply method.
* 1. Find the shortest string.<br>
* 2. Using two for loops, the outer loop's control condition is the length of the shortest string,
* and the memory loop's control condition is each element of the string array<br>
* */
public static String longestCommonPrefix_2(String[] strs) { if (strs == null || strs.length == 0)
return "";
// Find the shortest string.
String minString = strs[0];
for(int i=1;i<strs.length;i++) {
if(minString.length()>strs[i].length()) {
minString = strs[i];
}else {
continue;
}//end if
}//end for //Find whether other strings include the minString.
boolean isInclude = true;
StringBuilder str = new StringBuilder();
for (int j = 0; j < minString.length(); j++) {
for (int i = 0; i < strs.length-1; i++) {
if (strs[i].charAt(j) == strs[i + 1].charAt(j)) {
isInclude = true;
} else {
isInclude = false;
break;
}//end if
}// end for
if(isInclude==true){
str.append(minString.charAt(j));
}else if(isInclude == false&&j==0) {
break;
}//end if
}// end for return String.valueOf(str); }//end longestCommonPrefix() /**
* Answer online.
* Horizontal scanning
* */
public String longestCommonPrefix_3(String[] strs) {
if (strs.length == 0) return "";
String prefix = strs[0];
for (int i = 1; i < strs.length; i++)
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) return "";
}
return prefix;
}//end longestCommonPrefix() /**
* Answer online.
* Binary search
* */
public static String longestCommonPrefix_4(String[] strs) {
if (strs == null || strs.length == 0)
return "";
int minLen = Integer.MAX_VALUE;
for (String str : strs)
minLen = Math.min(minLen, str.length());
int low = 1;
int high = minLen;
while (low <= high) {
int middle = (low + high) / 2;
if (isCommonPrefix(strs, middle))
low = middle + 1;
else
high = middle - 1;
}
return strs[0].substring(0, (low + high) / 2);
}
/**aiding method */
private static boolean isCommonPrefix(String[] strs, int len){
String str1 = strs[0].substring(0,len);
for (int i = 1; i < strs.length; i++)
if (!strs[i].startsWith(str1))
return false;
return true;
}
}//end LongestCommonPrefix_14

Java实现LeetCode_0014_LongestCommonPrefix的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  3. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  4. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

  5. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  6. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  7. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  8. Java多线程基础学习(二)

    9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...

  9. Java多线程基础学习(一)

    1. 创建线程    1.1 通过构造函数:public Thread(Runnable target, String name){}  或:public Thread(Runnable target ...

随机推荐

  1. [hdu4497]分解质因数

    题意:求满足gcd(x,y,z)=G,lcm(x,y,z)=L的x,y,z的解的个数. 大致思路:首先如果L % G != 0那么无解,否则令u = L / G,问题变为,gcd(r,s,t)=1,l ...

  2. springBoot第二种配置文件yaml书写方式及读取数据、整合myBatis和整合junit

    一.yaml文件格式:key-value形式:可以表示对象 集合 1.语法:key:value 冒号后面必须跟一个空格再写value值 key1: key2: key3:value 2.属性取值:a. ...

  3. python --分隔符split()

    描述: python split()是通过指定分隔符对字符串进行切片,且可以指定分隔n+1个字符串. 语法: str.split(str="",num=string.count(s ...

  4. spring cloud系列教程第四篇-Eureka基础知识

    通过前三篇文章学习,我们搭建好了两个微服务工程.即:order80和payment8001这两个服务.有了这两个基础的框架之后,我们将要开始往里面添加东西了.还记得分布式架构的几个维度吗?我们要通过一 ...

  5. POJ3903 Stock Exchange LIS最长上升子序列

    POJ3903 Stock Exchange #include <iostream> #include <cstdio> #include <vector> #in ...

  6. JS的函数和对象四

    复习 数组 toString/join/concat/slice/splice/reverse/sort/ push/pop/unshift/shift 字符串 new String(2)  /  S ...

  7. ES6---面向对象上的简化

    一.ES6---面向对象上的简化 1.单个对象提供了2处简化: 1.1 如果对象的属性来自于对象外的变量,且变量名刚好和属性名相同.则不用写两遍相同的名字, 只要写一遍即可. 1.2 所有对象的方法, ...

  8. flask之CBV模式

    flask_cbv.py ''' flask中的CBV模式: (1)导入views模块: from flask import views (2)定义类,继承views.MethodView类: cla ...

  9. NPM的由来——为什么要使用NPM

    原文:https://blog.csdn.net/qq_37696120/article/details/80507178 https://blog.csdn.net/hong10086/articl ...

  10. mysql-connector-java 6版本的jdbc连接问题

    使用新版本6的jdbc驱动,会出现下面的问题 Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The s ...