LeetCode_14. Longest Common Prefix
14. Longest Common Prefix
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 ""
.
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z
.
package leetcode.easy; public class LongestCommonPrefix {
@org.junit.Test
public void test() {
String[] strs1 = { "flower", "flow", "flight" };
String[] strs2 = { "dog", "racecar", "car" };
System.out.println(longestCommonPrefix1(strs1));
System.out.println(longestCommonPrefix1(strs2));
System.out.println(longestCommonPrefix2(strs1));
System.out.println(longestCommonPrefix2(strs2));
System.out.println(longestCommonPrefix3(strs1));
System.out.println(longestCommonPrefix3(strs2));
System.out.println(longestCommonPrefix4(strs1));
System.out.println(longestCommonPrefix4(strs2));
} public String longestCommonPrefix1(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;
} public String longestCommonPrefix2(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
for (int i = 0; i < strs[0].length(); i++) {
char c = strs[0].charAt(i);
for (int j = 1; j < strs.length; j++) {
if (i == strs[j].length() || strs[j].charAt(i) != c) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
} public String longestCommonPrefix3(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
return longestCommonPrefix3(strs, 0, strs.length - 1);
} private String longestCommonPrefix3(String[] strs, int l, int r) {
if (l == r) {
return strs[l];
} else {
int mid = (l + r) / 2;
String lcpLeft = longestCommonPrefix3(strs, l, mid);
String lcpRight = longestCommonPrefix3(strs, mid + 1, r);
return commonPrefix(lcpLeft, lcpRight);
}
} String commonPrefix(String left, String right) {
int min = Math.min(left.length(), right.length());
for (int i = 0; i < min; i++) {
if (left.charAt(i) != right.charAt(i)) {
return left.substring(0, i);
}
}
return left.substring(0, min);
} public String longestCommonPrefix4(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);
} private 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;
}
}
LeetCode_14. Longest Common Prefix的更多相关文章
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- No.014:Longest Common Prefix
问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...
- 68. Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
随机推荐
- 使用C#的Flags特性
举个例子:我有如下的一个需求,当我想要取得用户信息的时候,会先从本地缓存中查找,找不到然后从分布式缓存中查找,最后找不到再从数据库中查询.但是有些场景我又不需要查询数据库. 所以我想建立如下这种模型. ...
- tp5.1动态获取器 增加一个不存在的字段
$list = $this->agent->where($where) ->withAttr('region',function ($value,$data){ $provice_n ...
- 关于静态资源放在CDN上
https://www.netlify.com/ https://app.netlify.com/signup?_ga=2.194141613.1097457726.1543799087-101005 ...
- 下载apache旗下Web服务器软件
apache官方网站:http://www.apache.org/ 1.百度搜索apache找到apache官网 2.进入apache官网打开旗下web服务器软件列表 3.进入apache旗下web服 ...
- C# 委托实现冒泡排序
委托实现员工根据工资升序排列 首先定义员工类 class Employee { public string Name { get; private set; } public decimal Sala ...
- [Google Guava] 7-原生类型
原文链接 译文链接 译者:沈义扬,校对:丁一 概述 Java的原生类型就是指基本类型:byte.short.int.long.float.double.char和boolean. 在从Guava查找原 ...
- [HTML5] Lazyload below the fold images and iframes with native browser lazy-loading
In this lesson, you'll learn how to use the loading="lazy" attribute available on images a ...
- 题解 [JOI 2019 Final] 硬币收藏
题面 解析 首先题目可以理解为把一些点放进一个框里,每个格子只能放一个. 那么显然你可以先把这个点移到框里离它最近的格子里, (这个时候格子里可以放很多个) 然后再在框里乱跑移动. 那么我们先考虑只有 ...
- HDU 6125 - Free from square | 2017 Multi-University Training Contest 7
思路来自这里 - - /* HDU 6125 - Free from square [ 分组,状压,DP ] | 2017 Multi-University Training Contest 7 题意 ...
- Appium自动化测试教程-自学网-安卓模拟器
安卓模拟器: 夜神模拟器安装配置 下载地址:https://www.yeshen.com 开启VT VT是什么?为什么要开启VT? VT,全称是Virtualization Technology,即是 ...