leetcode14
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的更多相关文章
- leetcode-14最长公共前缀
leetcode-14最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower& ...
- Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数  这两 ...
- [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- leetcode14:最长公共字符串
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- LeetCode14.最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- LeetCode14.最长公共前缀 JavaScript
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- leetcode14最长公共前缀
class Solution { public: string longestCommonPrefix(vector<string>& strs) { ) return " ...
- Leetcode14._最长公共前缀
题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow&q ...
随机推荐
- hdu2188巴什博弈
裸题,直接套公式 巴什游戏只是换了一个说法而已 #include<map> #include<set> #include<cmath> #include<qu ...
- 在请求的参数中设置可选值列表为当前职责可访问的所有OU
方法一: 实现此需求的前提之一是为该请求开启多业务实体访问,开启方法 系统管理员->系统管理->并发->程序,进入OAF页面,查询你的并发,然后点更新,选择请求,在业务实体模式下选择 ...
- 管道 && 消息队列 && 共享内存
http://blog.csdn.net/piaoairy219/article/details/17333691 1. 管道 管道的优点是不需要加锁. 缺点是默认缓冲区太小,只有4K. 一个管道只适 ...
- PHP和JAVA整合开发的三个方案(六)
php作为前端开发,java负责后台开发,这样取长补短的方案很适合现在web开发.现在PHP和JAVA整合开发比较好的方案只有3个:1.SOAP2.php-java-bridge3.Quercus Q ...
- [转载]c语言指针segmentation fault 指针常常错误的小地方
http://www.cnblogs.com/qingjoin/archive/2012/03/20/2408944.html #include <stdio.h> ] = ] = ] = ...
- hdu3572
题解: 网络流 判断是否为漫流 代码: #include <cstdio> #include <cstring> #include <algorithm> #inc ...
- HDFS读写流程
01.并行读取 02.逐个节点写入
- django-pure-pagination使用方法
1.pip install django-pure-pagination 安装包. 2.加入app: 'pure_pagination', 3.在view中写入分布逻辑. try: page = r ...
- tintColor 与 UIImage.renderingMode 渲染
在iOS7中,UIView新增了一个属性tintColor.这是一个UIColor,被使用在UIView中改变应用程序的外观的.默认tintColor的值为nil,这表示它将会运用父视图层次的颜色来进 ...
- Git 学习之 Git Basics
最近在用git,但git学习曲线实在是有点高. 好在找到一个文档 https://www.atlassian.com/git/tutorial/,以下就是学习笔记吧! git init git ini ...