问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3921 访问。

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""。

输入: ["flower","flow","flight"]

输出: "fl"

输入: ["dog","racecar","car"]

输出: ""

解释: 输入不存在公共前缀。

说明:所有输入只包含小写字母 a-z 。


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 "".

Input: ["flower","flow","flight"]

Output: "fl"

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.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3921 访问。

public class Program {

    public static void Main(string[] args) {
var strs = new string[] { "flower", "flow", "flight" }; var res = LongestCommonPrefix(strs);
Console.WriteLine(res); Console.ReadKey();
} private static string LongestCommonPrefix(string[] strs) {
if(strs.Length == 0) return "";
if(strs.Length == 1) return strs[0];
var min = int.MaxValue;
foreach(var item in strs) {
if(item.Length < min) min = item.Length;
}
var index = -1;
for(var i = 0; i < min; i++) {
for(var j = 1; j < strs.Length; j++) {
if(strs[j][i] != strs[0][i]) return strs[0].Substring(0, i);
else {
index = i;
}
}
}
return strs[0].Substring(0, index + 1);
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3921 访问。

fl

分析:

设数组的长度为 n,单词的最大长度为 m,那么显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#14-最长公共前缀​​​​​​​(Longest Common Prefix)的更多相关文章

  1. LeetCode 14. 最长公共前缀(Longest Common Prefix)

    14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...

  2. #leetcode刷题之路14-最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  3. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

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

  4. Leetcode题库——14.最长公共前缀

    @author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...

  5. 【leetcode算法-简单】14. 最长公共前缀

    [题目描述] 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","fl ...

  6. 【Leetcode】【简单】【14最长公共前缀】【JavaScript】

    题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

  7. Java实现 LeetCode 14 最长公共前缀

    14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&quo ...

  8. 最长公共子串(Longest common substring)

    问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子串.(子串中的字符要求连续) 这道题和最长公共 ...

  9. python刷LeetCode:14. 最长公共前缀

    难度等级:简单 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

  10. [LeetCode]14.最长公共前缀(Java)

    原题地址: longest-common-prefix 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入:st ...

随机推荐

  1. Ethical Hacking - Web Penetration Testing(13)

    OWASP ZAP(ZED ATTACK PROXY) Automatically find vulnerabilities in web applications. Free and easy to ...

  2. 五大高效的PDF文件搜索引擎

    当你花了半个多小时在线搜索PDF文档,却发现您找到的文档都不是您需要的PDF格式.如前说述,您可以先打开PDF文档查看是不是PDF格式的,然后再到web浏览器中下载该文档.那么,为了确保您获得的文档是 ...

  3. F - Maximal Intersection --------暴力求解题

    You are given n segments on a number line; each endpoint of every segment has integer coordinates. S ...

  4. 小白入门python新手教程python

    python教程很多,但是需要自学教程更好一些,看自学python教程3遍,然后一步步操作,7天后就会有很大的收货. 要向数据处理方向走,数据处理需要网络爬虫的知识,且更加精进.下面是我从网上查找这方 ...

  5. GridMask:一种数据增强方法

    GridMask Data Augmentation, ARXIV 2020 代码地址:https://github.com/akuxcw/GridMask 这篇论文提出了一种简单的数据增强方法,在图 ...

  6. numpy的random方法和常用数据类型

    NumPy 的常用数据类型 np.random 随机数模块

  7. PHP easter_days() 函数

    ------------恢复内容开始------------ 实例 输出不同年份的复活节与 3 月 21 日之间的天数: <?phpecho "Easter Day is " ...

  8. PHP jdtojulian() 函数

    ------------恢复内容开始------------ 实例 把儒略历法的日期转换为儒略日计数,然后再转换回儒略历法的日期: <?php$jd=juliantojd(6,20,2007); ...

  9. PHP preg_match() 函数

    preg_match 函数用于执行一个正则表达式匹配.高佣联盟 www.cgewang.com 语法 int preg_match ( string $pattern , string $subjec ...

  10. C/C++编程笔记:C语言预处理命令是什么?不要以为你直接写#就行!

    很多小伙伴在自己写代码的时候,已经多次使用过#include命令.使用库函数之前,应该用#include引入对应的头文件.其实这种以#号开头的命令称为预处理命令. C语言源文件要经过编译.链接才能生成 ...