C#LeetCode刷题之#14-最长公共前缀(Longest Common Prefix)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- #leetcode刷题之路14-最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- Leetcode题库——14.最长公共前缀
@author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...
- 【leetcode算法-简单】14. 最长公共前缀
[题目描述] 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","fl ...
- 【Leetcode】【简单】【14最长公共前缀】【JavaScript】
题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- Java实现 LeetCode 14 最长公共前缀
14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&quo ...
- 最长公共子串(Longest common substring)
问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子串.(子串中的字符要求连续) 这道题和最长公共 ...
- python刷LeetCode:14. 最长公共前缀
难度等级:简单 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- [LeetCode]14.最长公共前缀(Java)
原题地址: longest-common-prefix 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入:st ...
随机推荐
- java面试题NIO与OIO的区别
面向流与面向缓冲 Java NIO和IO之间第一个最大的区别是,IO是面向流的,NIO是面向缓冲区的. Java IO面向流意味着每次从流中读一个或多个字节,直至读取所有字节,它们没有被缓存在任何地方 ...
- GPO - Folder Mapping via GPO
Create a Group Policy on AD DC Server. The GPO policy will come into effect on the next login, or us ...
- Python Hacking Tools - Password Sniffing
Password Sniffing with Scapy 1. Download and install the Scapy first. pip install scapy https://scap ...
- 足球动图gif(一)
- Python3 生成器解析
第6章 函数 6.1 函数的定义和调用 6.2 参数传递 6.3 函数返回值 6.4 变量作用域 6.5 匿名函数(lambda) 6.6 递归函数 6.7 迭代器 6.8 生成器 6.9 装饰器 6 ...
- Linux安装禅道项目管理软件
1.从官网上面下载禅道的rpm文件 #wget http://dl.cnezsoft.com/zentao/7.1/zentaopms-7.1.stable-1.noarch.rpm 2.用指令安装 ...
- ffmpeg获取视频封面图片
ffmpeg百度云盘下载地址:https://pan.baidu.com/s/1Am_x0abBvXTrbCFPHs5e9Q 提取码:4y2r /** * ffmpeg百度云盘下载地址:https:/ ...
- 「从零单排canal 06」 instance模块源码解析
基于1.1.5-alpha版本,具体源码笔记可以参考我的github:https://github.com/saigu/JavaKnowledgeGraph/tree/master/code_read ...
- 重置spyder 解决 gbk 编码不能读取问题
重置spyder 解决 gbk 编码不能读取问题 2020-06-18
- Django学习路9_流程复习
以下图示为 学习过程中,在千锋教育视频上截图的 视频链接: https://www.bilibili.com/video/BV1rx411X717?p=11 2 ...