public class Solution {
public string LongestCommonPrefix(string[] strs) {
if (strs.Length == )
{
return "";
}
else if (strs.Length == )
{
return strs[];
}
else
{
var maxLen = ; var len = strs.Length;
while (true)
{
for (int i = ; i < len - ; i++)
{
var s1 = strs[i];
var s2 = strs[i + ]; if (s1.Length < maxLen || s2.Length < maxLen)
{
return strs[].Substring(, maxLen - );
} if (s1.Substring(, maxLen) != s2.Substring(, maxLen))
{
return strs[].Substring(, maxLen - );
}
}
maxLen++;
}
}
}
}

https://leetcode.com/problems/longest-common-prefix/#/description

补充一个python的实现:

 class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
n = len(strs)
if n == :
return ''
if n == :
return strs[]
j =
while True:
for i in range(,n):
if j >= len(strs[i-]) or j >= len(strs[i]):
return strs[i][:j]
if strs[i-][j] != strs[i][j]:
return strs[i][:j]
j +=
return strs[][:j]

leetcode14的更多相关文章

  1. leetcode-14最长公共前缀

    leetcode-14最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower& ...

  2. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  3. LeetCode14 Longest Common Prefix

    题意: Write a function to find the longest common prefix string amongst an array of strings. (Easy) 这两 ...

  4. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  5. leetcode14:最长公共字符串

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  6. LeetCode14.最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  7. LeetCode14.最长公共前缀 JavaScript

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  8. leetcode14最长公共前缀

    class Solution { public: string longestCommonPrefix(vector<string>& strs) { ) return " ...

  9. Leetcode14._最长公共前缀

    题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow&q ...

随机推荐

  1. UVA-11613 Acme Corporation (最大费用最大流+拆点)

    题目大意:有一种商品X,其每每单位存放一个月的代价I固定.并且已知其每月的最大生产量.生产每单位的的代价.最大销售量和销售单价,还已知每个月生产的X能最多能存放的时间(以月为单位).问只考虑前m个月, ...

  2. 044——VUE中组件之使用内容分发slot构建bootstrap面板panel

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Python3 字符串(七)

    字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. python中单引号和双引号使用完全相同. 使用三 ...

  4. 理解HTTP之Content-Type

    http://homeway.me/2015/07/19/understand-http-about-content-type/

  5. IOS NSCharacterSet 去除NSString中的空格

    去除 username中的空格,table newline,nextline 代码如下: NSCharacterSet *whitespace = [NSCharacterSet  whitespac ...

  6. C++可调用对象与函数表

    c++的可调用对象 有 函数 函数指针 lambda表达式 bind的对象 重载了函数调用运算符的类 如何调用? 函数调用 void afuncToCall() { cout << &qu ...

  7. notification的创建及应用

    之前我用了button.setonclicklistener来获取一个点击事件,但是在new notificationcompat.builder是会报一个没有定义的错误.这种点击事件的方式就不会报那 ...

  8. C#典型案例及分析

    1.简单工厂计算器

  9. vue项目之webpack打包静态资源路径不准确

    摘自:https://blog.csdn.net/viewyu12345/article/details/83187815 问题 将打包好的项目部署到服务器,发现报错说图片找不到. 静态资源如js访问 ...

  10. 【转】Ubuntu13.04配置:Vim+Syntastic+Vundle+YouCompleteMe

    原文网址:http://www.cnblogs.com/csuftzzk/p/3435710.html 序言 使用Ubuntu和vim已经有一段时间了,对于Vim下的插件应用,我总是抱着一股狂热的态度 ...