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 ...
随机推荐
- Ethical Hacking - NETWORK PENETRATION TESTING(9)
WEP Cracking Packet Injection What if the AP was idle, or had no clients associated with it? In this ...
- cmd : 代理设置/检验代理设置成功
设置代理很简单,一句话的事儿. set HTTP_PROXY=http://user:password@proxy.domain.com:port 比如说,我用ssr,默认地址是127.0.0.1:1 ...
- vue 应用 :多语言显示
<template> <div class="hello2"> <page-content> </page-content> < ...
- FaaS 给前端带来了什么?
一.Serverless 与 FaaS Serverless 是一种云计算理念,即无服务器计算(Serverless Computing): Serverless suggests that the ...
- React Native 控制一个component的显示隐藏
// 首先在constructor里: this.state = { visible: false } // 然后在点击事件设置: this.setState({ visible: t ...
- 手写IOC容器
IOC(控制翻转)是程序设计的一种思想,其本质就是上端对象不能直接依赖于下端对象,要是依赖的话就要通过抽象来依赖.这是什么意思呢?意思就是上端对象如BLL层中,需要调用下端对象的DAL层时不能直接调用 ...
- MacOS英语学习
总结于B站Mac云课堂:https://www.bilibili.com/video/BV1vf4y1U7SZ 各个软件的链接: Edge:https://www.microsoft.com/zh-c ...
- PHP zip_entry_filesize() 函数
定义和用法 The zip_entry_filesize() 函数返回 zip 档案项目的原始文件尺寸(在压缩之前).高佣联盟 www.cgewang.com 语法 zip_entry_filesiz ...
- ThinkPHP6 核心分析之应用程序初始化
runWithRequest () 方法 在 Http 类的 run() 方法中,得到 think\Request 类的实例后,程序接着执行 $response = $this->runWith ...
- 当asp.net core偶遇docker一(模型验证和Rabbitmq 二)
上一篇我们说到构建了一个Rabbitmq容器 现在我们说说如何在一个悄悄传输消息到队列 我们现在设计一个Rabbitmq发送消息部分的模块 先设计一个远程发送的接口 public interface ...