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. 配置类为什么要添加@Configuration注解呢?

    配置类为什么要添加@Configuration注解呢? 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 推荐阅读: Spring官网阅读 | 总结篇 Spring ...

  2. gather函数

    gather(input, dim, index):根据  index,在  dim  维度上选取数据,输出的  size  与  index  一致 # input (Tensor) – 源张量 # ...

  3. 下载Android代码

    1.由于墙,无法下载android源码,但是又不想利用清华源repo下载整个工程,只下载个别仓库 解决办法: 2.下载frameworks/base: git clone https://androi ...

  4. OpenCV开发笔记(五十六):红胖子8分钟带你深入了解多种图形拟合逼近轮廓(图文并茂+浅显易懂+程序源码)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  5. Gitlab 修改ldap认证

    1. 备份数据 2. 修改配置 使用自己搭建的openldap 使用用户中心的openldap 说明:base属性执行所有员工,user_filter属性主要用来实现分组功能.上面的配置是只有ldap ...

  6. storm-redis 详解

    多的不说,先来代码分析,再贴我自己写的代码.如果代码有错误,求更正.. 导入两个关键包,其他项目需要的包,大家自己导入了,我pom下的包太多,不好一下扔上来. <dependency> & ...

  7. 马兴德201771010117《面向对象程序设计(java)》第一周学习总结

    第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com 挽歌朽年 程序设计评测:https://pintia.cn/ 791683057@qq ...

  8. B. Sleepy Game 博弈搜索

    题意:给一个有向图和起点,然后只有一名选手,这名选手可以随意挪动棋子,最终不能动的时候走过的边为奇数边为Win并输出路径,否则如果有环输出Draw,否则输出Lose; 题目链接 知道状态数最多只有n* ...

  9. hdu5967数学找规律+逆元

    Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  10. 【Mac】屏蔽系统升级更新

    三行代码解决Mac升级弹窗,小红点数字1