C# 写 LeetCode easy #14 Longest Common Prefix
14、Longest Common Prefix
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
.
代码:
static void Main(string[] args)
{
string[] str = new string[] { "my","myname","mys"};
string res=GetLongestCommonPrefix(str);
Console.WriteLine(res);
Console.ReadKey();
} private static string GetLongestCommonPrefix(string[] strs)
{
if (strs.Length == ) return "";
if (strs.Length == ) return strs[];
var min = int.MaxValue;
foreach (var item in strs)
{
if (item.Length < min)
{
min = item.Length;
}
}
var index = -;
for (int i=; i < min; i++)
{
for (int j = ; j < strs.Length; j++)
{
if (strs[j][i] != strs[][i])
{
return strs[].Substring(, i);
}
else
{
index = i;
}
}
}
return strs[].Substring(,index+);
}
解析:
输入:字符串数组
输出:字符串
思想:
首先,分三部分,当数组为空时,返回空字符串;数组中只有一个元素,输出该元素;数组中有若干字符串,则进行以下操作。(注意:这里也可以判断数组中是否含有空字符串,有则返回空)
其次,对于一般情况,先找出数组中最小长度的字符串,根据这个长度,从第一个字符开始遍历。
最后,将数组中的每个字符串和第一个字符串的每个字符进行比较。相同则记录字符索引值,否则返回共同子串。
时间复杂度:O(m*n) 其中m是单词最大长度,n是数组长度。
C# 写 LeetCode easy #14 Longest Common Prefix的更多相关文章
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 【LeetCode】14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...
- 【LeetCode】14. Longest Common Prefix (2 solutions)
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 《LeetBook》leetcode题解(14):Longest Common Prefix[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
随机推荐
- 新的开发域名 fastadmin.tk
新的开发域名 fastadmin.tk 这个的所有子域名批向 127.0.0.1,可以用于开发. 以后不用再修改系统的 hosts. 使用案例 手把手教你安装 FastAdmin 到虚拟主机 (php ...
- HIVE-如何查看执行日志
HIVE既然是运行在hadoop上,最后又被翻译为MapReduce程序,通过yarn来执行.所以我们如果想解决HIVE中出现的错误,需要分成几个过程 HIVE自身翻译成为MR之前的解析错误 Hado ...
- wpf中为DataGrid添加checkbox支持多选全选
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...
- FPGA中竞争冒险问题的研究
什么是竞争冒险? 1 引言 现场可编程门阵列(FPGA)在结构上由逻辑功能块排列为阵列,并由可编程的内部连线连接这些功能块,来实现一定的逻辑功能. FPGA可以替代其他PLD或者各种中小规模数 ...
- POJ3321(dfs序列+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25711 Accepted: 7624 Descr ...
- Java基础--比较器Comparator
Comparable接口和Comparator接口都是用来定义集合中的排序的,只是Comparable是在集合内部定义排序的实现,Comparator是在集合外部排序的实现. Comparable 的 ...
- Day2-VIM(二):插入
基础 字符位置插入 i 在光标之前插入 a 在光标之后追加 你看,其实刚开始用这两个就足够了,这就是最基础的 为什么这么说呢?因为你可以依靠上一节中的移动命令来达到任意位置,然后再大力插入 不要忘了, ...
- FLASH和EEROM使用【转】
最近在看代码的时候,遇到了一个使用FLASH模拟EEPROM的情况,看到这个我当时是一脸蒙蔽啊,对于一个有时候连FLASH和EEPROM都分不清的人来说,怎么可能读懂用FLASH来模拟EEPROM呢? ...
- Python:包、模块、类、函数的调用
一.关系 包一般指文件夹或者安装包(安装包一般也是压缩后的文件夹),里面包含多个.py文件(必须有一个__init__.py文件),一般也含有多个子包(或子文件夹): 一般一个.py文件就是一个模块, ...
- Task用法(1)-启动方法
第一.基本使用 Thread,ThreadPool,Task的区别 Task是.NET4.0加入的,跟线程池ThreadPool的功能类似,用Task开启新任务时,会从线程池中调用线程,而Thread ...