Java实现LeetCode_0014_LongestCommonPrefix
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的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
随机推荐
- Akko海洋之星
今天(2020.5.14)入手Akko海洋之星84茶轴,开心呀~~ 考虑方面: 价格>键线分离>接线接口>轴体>键帽>材质 价格: 200~500之间入门级cherry轴 ...
- 集群、分布式、SOA、微服务、webService等思想的整理
引子:前几天甲方问我,他用wpf弄个界面,能不能通过其他语言给他传输数据,我由此想到了webservice(此时此刻,我也没有用过webServices),作日翻阅了一些资料,对这块技术有了个大概的了 ...
- tomcat 8.5 及其 9.0 response写cookie 设置damain为 [.test.com] 出错 An invalid domain [.test.com] was specified for this cookie
抛出异常: java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cooki ...
- curl发送请求
一.get请求 curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...
- 前端面试题-TCP和UDP的区别
TCP和UDP的区别 (1)TCP是面向连接的,udp是无连接的即发送数据前不需要先建立链接. (2)TCP提供可靠的服务.也就是说,通过TCP连接传送的数据,无差错,不丢失,不重复,且按序到达;UD ...
- java基础一(标识符、数据类型及注释)
1.标识符 标识符可以由字母.数字(不能以数字开头).下划线(_).美元符($)组成: 标识符不能包含 @.%.空格等其它特殊字符: 标识符区分大小写: 2.数据类型 int->整型->4 ...
- VST的安装
对需要使用VST的用户,你可以到http://www.soft-gems.net/去免费下载没有使用限制.没有广告的VST.包括例子程序以及说明文档也可以下载到,下载完成后,就是安装,以前版本的VST ...
- Spring AOP实现接口验签
因项目需要与外部对接,为保证接口的安全性需要使用aop进行方法的验签; 在调用方法的时候,校验外部传入的参数进行验证, 验证通过就执行被调用的方法,验证失败返回错误信息: 不是所有的方法都需要进行验签 ...
- Appium自动化(4) - Appium Desired Capabilities 参数详解
如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html Desired Capabilit ...
- python机器学习(四)分类算法-决策树
一.决策树的原理 决策树思想的来源非常朴素,程序设计中的条件分支结构就是if-then结构,最早的决策树就是利用这类结构分割数据的一种分类学习方法 . 二.决策树的现实案例 相亲 相亲决策树 ...