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++语法备忘
记录一些C++的语法方便日后查看. 1.C++初始化语法 C++中新增加了两种初始化语法,其中大括号初始化器需要C++11以上的实现,使用时可以加等号,也可以不加,而且大括号中可以不包含任何东西,这种 ...
- tsp问题-遍历算法/随机算法
旅行商问题,即TSP问题(Traveling Salesman Problem)又译为旅行推销员问题.货郎担问题,是数学领域中著名问题之一.假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路 ...
- SEO 统计算法
1)简单粗暴型的,这里不用去管浏览器的user-agent,不管cookie等信息,每产生一次PV,就直接计数,优点:简单,缺点:可能不真实,也可能有刷量数据 2) 稍微细腻点的统计,会区分新老用户, ...
- 0001SpringBoot整合Mybatis
SpringBoot整合Mybatis主要分为以下几个步骤: 1.添加Mybatis的起步依赖(pom.xml) 2.添加数据库驱动坐标(pom.xml) 3.添加数据库连接信息(applicatio ...
- Mybatis 动态SQL注解 in操作符的用法
在SQL语法中如果我们想使用in的话直接可以像如下一样使用: ,,) ; ,,) ; 但是如果在MyBatis中的使用 in 操作符,像下面这样写的话,肯定会报错: @Update("upd ...
- 2018 开始认真学习点python
2018 伊始,又是春暖花开.俗语,“一年之计在于春”.又是一年立志时. 决定认真学习一些web. 本来倾向与学习NodeJS的.可是之前买的python的书太多了.就先紧手头的资源看了再说吧. 今天 ...
- SpringBoot+JTA+Mybatis
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/sinat_36596988/article ...
- [Luogu] U18430 萌萌的大河
https://www.luogu.org/problemnew/show/U18430 思路比较好想 树链剖分 对于1操作 只需将以该点为根的子树打标记,将所有数存入数组排序 每次进行1操作时,判断 ...
- 【原创】go语言学习(一)
一.go发展历史 1.1诞生历史 1.诞生于2006年1月下午15点4分5秒 2.2009发布并正式开园 3.2012年第一个正式版本Go1.0发布 4.截止2019年10月8日,Go1.13.1 1 ...
- 将省市县三级联动的json数据,转化为element-ui能用的格式,并使用
var options=[]; var cities = { '北京': { '北京': ['东城区', '西城区', '崇文区', '宣武区', '朝阳区', '丰台区', '石景山区', '海淀区 ...