14. Longest Common Prefix

Easy

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的更多相关文章

  1. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  2. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  3. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  4. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

  5. 14. Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...

  6. Leetcode Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...

  7. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  8. No.014:Longest Common Prefix

    问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...

  9. 68. Longest Common Prefix

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

随机推荐

  1. webpack 和 code splitting

    Code Splitting指的是代码分割,那么什么是代码分割,webpack和code splitting又有什么样的联系呢? 使用npm run dev:"webpack-dev-ser ...

  2. Python 编码encode()、 解码decode()问题

    乱码这种东西,时不时出现.本来开开心心想着我要学习啦,然后兴高采烈打开了比火星文还火星文的字符-- 没事,我可以搞定这堆鬼画符. 先来讲一下为什么有乱码这种东西的存在 故事是这样滴: 字符串是Pyth ...

  3. 问题 C: 如沫春风 ---有毒的gets(),新OJ不能用!用scanf(%s)读入即可!——ZZNU新OJ

    问题 C: 如沫春风 时间限制: Sec 内存限制: MB 提交: 解决: [提交] [状态] [讨论版] [命题人:admin] 题目描述 月亮很亮,亮也没用,没用也亮. 我喜欢你,喜欢也没用,没用 ...

  4. 优先级:content –> width –> flex-basis (limted by max|min-width)

    原文: https://www.jianshu.com/p/17b1b445ecd4 -------------------------------------------- 最近在学习Flex Bo ...

  5. [Google Guava] 4-函数式编程

    原文链接 译文链接 译者:沈义扬,校对:丁一 注意事项 截至JDK7,Java中也只能通过笨拙冗长的匿名类来达到近似函数式编程的效果.预计JDK8中会有所改变,但Guava现在就想给JDK5以上用户提 ...

  6. Shiro (包含权限满足其中一个就通过的用法)

    方法/步骤 1 web.xml添加配置 <!-- shiro过滤器 --> <filter> <filter-name>shiroFilter</filter ...

  7. VUE 异步数据传递给 component props 的问题

    案例一 父组件parent.vue // asyncData为异步获取的数据,想传递给子组件使用 <template> <div> 父组件 <child :child-d ...

  8. python 序列通用操作

    通用序列操作:索引:greeting=hellogreeting[0] 分片:number[1,2,3,4,5,6]number[3:6]number[3:6:1] 序列相加:[1,2,3] + [4 ...

  9. RNN(一)——RNN和LSTM原理

    背景 神经网络,卷积神经网络等其他深度学习算法,都有个局限性,各个输入在算法内部是相对独立的.比如:'星际争霸有意思,我爱玩'这句话,是有上下文关系的. 如果放在其他网络里面,各个分词将会独立处理.但 ...

  10. 2019.6.28 校内测试 T2 【音乐会】二重变革

    看到这个题之后,一个很暴力很直接的想法就是贴上题目中的代码然后交上去走人,但是很显然这是会TLE+MLE的,想想谁会这么傻把主要代码给你QwQ~: 其实这段代码是想告诉你一件事:用序列中的大数减去小数 ...