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. NLP传统基础(1)---BM25算法---计算文档和query相关性

    一.简介:TF-IDF 的改进算法 https://blog.csdn.net/weixin_41090915/article/details/79053584 bm25 是一种用来评价搜索词和文档之 ...

  2. python_网络编程struct模块解决黏包问题

    为什么会出现黏包现象: 首先只有在TCP协议中才会出现黏包现象,是因为TCP协议是面向流的协议,在发送的数据传输的过程中还有缓存机制来避免数据丢失,因此,在连续发送小数据的时候,以及接收大小不符的时候 ...

  3. for循环使用后contains方法失去效果

    重写了cartInfo中的equals和hashcode方法 第一种方式:包含是可以的List<CartInfo> cookie = new ArrayList<CartInfo&g ...

  4. 第82题:删除排序链表中的重复元素II

    一. 问题描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5 ...

  5. 第81题:搜索旋转排序数组II

    一. 问题描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判断给定的目标值是 ...

  6. 用Qt 画一个心形

    MainWindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTi ...

  7. 「ZJOI2014」力 FFT

    FFTl裸题,小于的部分直接做,大于的部分倒序后再做就行了. #include <bits/stdc++.h> using namespace std; const int MAXN = ...

  8. DOM查找

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. redis与spring整合实例

    1)首先是redis的配置. 使用的是maven工程,引入redis与spring整合的相关jar包 <!-- redis服务 start--> <dependency> &l ...

  10. learning express step(一)

    first : create new project then install express package : npm install express --savenpm WARN saveErr ...