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.

求几个字符串的公共前缀,两种方法

(1)暴力

利用两层循环,先取出第一个字符串的第i个字符,然后比较s[1]-s[n]的第i个字符是否相等

public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
String res ="";
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 res;
}
}
res =res+c;
}
return res;
}
}

(2)排序后比较首尾

字符串比较的结果,按照首个不同字符的大小排序,比如ab就排在ac的前面

["flower","flow","flight"]比较的结果是[flight,flow,flower],我们统计首尾,fl是最长前缀
["dog","racecar","car"]比较的结果是[car,dog,racecar]
根据这个特性,假如存在最长公共前缀,s[0]和s[n]之间的最长公共前缀就是我们要求的结果,因为排序结果是按照第一个非公共字符开始的
这里我们把过程简化到只要比较首尾字符,而不用比较所有的
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
Arrays.sort(strs);
int i = 0, len = Math.min(strs[0].length(), strs[strs.length - 1].length());
while (i < len && strs[0].charAt(i) == strs[strs.length - 1].charAt(i)) i++;
return strs[0].substring(0, i);
}
}

[LeetCode]14. Longest Common Prefix最长公共前缀的更多相关文章

  1. [leetcode]14. Longest Common Prefix 最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  2. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  3. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

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

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  5. 【LeetCode】Longest Common Prefix(最长公共前缀)

    这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...

  6. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  7. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  8. Longest Common Prefix -最长公共前缀

    问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...

  9. Leetcode 14. Longest Common Prefix(水)

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

随机推荐

  1. 装饰器设计模式初探及Java中实际应用举例

    本篇随笔主要介绍用Java实现简单的装饰器设计模式: 先来看一下装饰器设计模式的类图:  从图中可以看到,我们可以装饰Component接口的任何实现类,而这些实现类也包括了装饰器本身,装饰器本身也可 ...

  2. Qt 学习之路 2(17):文件对话框

    Home / Qt 学习之路 2 / Qt 学习之路 2(17):文件对话框 Qt 学习之路 2(17):文件对话框  豆子  2012年9月24日  Qt 学习之路 2  85条评论 在前面的章节中 ...

  3. elasticsearch 相关命令

    ubuntu server 启动命令:$ ./bin/elasticsearch 后台运行:$ ./bin/elasticsearch -d 查看当前进程:$ jps 启动后检查是否成功命令:$ cu ...

  4. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_8.1实例构造器和类A

    public class SomeType { } //等价于 public class SomeType { public SomeType():base(){} } [解释]如果定义的类没有显示定 ...

  5. Gym - 101572D Distinctive Character bfs 思维

    题目传送门 题目大意: 给出n个01串,让你构造一个字符串,使这个字符串和这些字符串中相似程度最高 尽可能低.如果两个字符串对应位置相同,则相似程度加一. 思路: 每一个01串更改自己的一部分后,都可 ...

  6. hive中解析json数组

    -- hive中解析json数组 select t1.status ,substr(ss.col,,) as col ,t3.evcId ,t3.evcLicense ,t3.evcAddress , ...

  7. 电脑c盘爆满检查与设置

    C盘文件爆满,先检查是那个文件造成的 操作方法:打开文件夹选项,将“隐藏受保护的操作系统文件(推荐)”前的勾选去掉,然后打开系统盘查看是那个文件占用空间大 Hiberfil.sys文件删除方法: 1. ...

  8. mapreduce去重

    现有一个某电商网站的数据文件,名为buyer_favorite1,记录了用户收藏的商品以及收藏的日期,文件buyer_favorite1中包含(用户id,商品id,收藏日期)三个字段,数据内容以“\t ...

  9. java——如何通过class调用该类的方法并获得返回值?(反射)

    demo: public class T{ public static void main(String[] args) throws Exception{ //获得Person的Class对象 Cl ...

  10. vue 的watch用法

    参考转自https://www.imooc.com/article/details/id/28187 类型:{ [key: string]: string | Function | Object | ...