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. docker容器的服务发现:consul

    官网:https://www.consul.io 官网文档:https://www.consul.io/docs简介 consul是一个服务发现的组件,在docker世界中他比较流行,主要是consu ...

  2. Linux基础命令-文本文件查看工具

    文本文件查看工具 cat concatenate 文本文件查看工具 cat /etc/fstab cat [OPTION]... [FILE]... -n:给显示的文本行编行 -E:显示行结束符 ta ...

  3. win32 获取本机网卡信息(MAC地址,IP地址等)

    由于一个需求需要获取网卡的MAC地址,就搜了一下,大部分都是COPY来COPY去的一些代码,有很多甚至不能直接运行或有还有内存泄漏.自己查了一下MSDN然后封装了一下: 需要注意,一个机器可能有多个网 ...

  4. FMDB是iOS平台的SQLite数据库框架

    1.FMDB简介 什么是FMDBFMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 为什么使用FMDB使用起来更加面向对象,省去了很多麻烦.冗余的C语言 ...

  5. Rails的静态资源管理(六)—— Asset Pipeline缓存存储方式、预处理、升级等

    官方文档:http://guides.ruby-china.org/asset_pipeline.html http://guides.rubyonrails.org/asset_pipeline.h ...

  6. 解决django不能以本机ip地址访问

    在使用django框架来架设网站时,我们测试一般是通过django的开发服务器来完成,但是我们可以看到生成的地址是127.0.0.1:8000这样的话,我们在外网就无法访问了. 解决办法是通过传入第三 ...

  7. Windows条件变量

    详细见MSDN:http://msdn.microsoft.com/en-us/library/windows/desktop/ms686903%28v=vs.85%29.aspx 我们已经看到,当想 ...

  8. 12-22C#公共控件(基本功能)

    在C#窗体中,公共控件的基本功能: 1.获取.设置控件的参数值: 2.事件(其实是一种特殊的方法和属性,当被其他外力触发它,就会发生,类似数据库的触发器.) 下面是基本的公共控件: 1.复选框 1)设 ...

  9. VS调试程序时,程序出现异常,但VS不报错的解决方案

    在调试>异常> 里面把勾全勾上就行了!

  10. ViewPager的使用方法

    首先是 导入jar包   下载地址:android-support-v4.jar 布局文件里添加viewPager布局 [html] view plaincopy <android.suppor ...