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的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  3. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

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

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

  5. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  6. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

  7. 【LeetCode】14. Longest Common Prefix (2 solutions)

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

  8. 《LeetBook》leetcode题解(14):Longest Common Prefix[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

随机推荐

  1. BZOJ4605:崂山白花蛇草水

    浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...

  2. 蓝桥杯 算法训练 ALGO-117 友好数

    算法训练 友好数   时间限制:1.0s   内存限制:256.0MB 问题描述 有两个整数,如果每个整数的约数和(除了它本身以外)等于对方,我们就称这对数是友好的.例如: 9的约数和有:1+3=4 ...

  3. [转] pip镜像升级报警 -trust-host问题解决方案

    pip升级到7.0以后,在使用http镜像进行包安装及升级的时候往往会有如下提示: Collecting beautifulsoup4The repository located at mirrors ...

  4. tomcat正常启动但是访问 404

    最近遇到了一些奇葩的的问题,搞了好半天才处理掉.今天就简单记录一下吧,以备不时之需.  问题描述: 在整合spring mvc项目的完成后,正常启动tomcat,发现tomcat启动成功了,但是访问本 ...

  5. Git命令之创建版本

    安装 安装好Git后,将会在桌面生成 这样一个图标 运行后将会是类似控制台程序的黑色窗口,其中mingw64(参考百度百科).这样的话就可以在输入命令 例如 :git 见到下图有详细的用法表示成功否则 ...

  6. 【转】onclick事件与href='javascript:function()'的区别

    href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...

  7. c语言-单链表(二)

    继续复习链表知识点,本章包含单链表的增加,删除,判断是否为空,和链表长度,以及链表的排序 几个知识点 1.链表的判断是否为空 //1.判断链表是否为空 bool isempty_list(PNODE ...

  8. qt安装必要的库 qt开源安装包下载

    yum install mesa-libGL-devel mesa-libGLU-devel #yum install freeglut-devel http://www.qt.io/download ...

  9. ORACLE——日期时间格式化参数详解 之一

    2.日期格式化参数详解 2.1 -/,.;: 指定返回字串分隔符 SQL> select to_char(sysdate,'yyyy.mm.dd') from dual; TO_CHAR(SYS ...

  10. pipeline(管道的连续应用)

    # -*- coding: utf-8 -*- """ Created on Tue Aug 09 22:55:06 2016 @author: Administrato ...