LeetCode -- 求字符串数组中的最长公共前缀
题目描写叙述:
Write a function to find the longest common prefix string amongst an array of strings.
就是给定1个字符串数组,找出公共最长前缀。
思路非常直接。使用1个索引来存最长公共前缀的长度就能够了。
注意, 假设使用1个字符串变量来存前缀的话,是不能AC的,由于题目不同意使用额外的空间。
public string LongestCommonPrefix(string[] strs) {
if(strs == null || strs.Length == 0){
return string.Empty;
}
if(strs.Length == 1){
return strs[0];
}
var index = 0;
for(var j = 0;j < strs[0].Length; j++){
for(var i = 1;i < strs.Length; i++){
if(j >= strs[i].Length || strs[0][j] != strs[i][j]){
return strs[0].Substring(0,index);
}
}
index++;
}
return strs[0].Substring(0 ,index);
}
LeetCode -- 求字符串数组中的最长公共前缀的更多相关文章
- 面试题:编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。(c++实现)
实例说明 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- 【leetcode 简单】第五题 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- leetcode腾讯精选练习之最长公共前缀(九)
最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower"," ...
- 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 ...
- LeetCode 14 Longest Common Prefix(最长公共前缀)
题目链接:https://leetcode.com/problems/longest-common-prefix/?tab=Description Problem: 找出给定的string数组中最 ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- POJ 2774 求两个串的最长公共前缀 | 后缀数组
#include<cstdio> #include<algorithm> #include<cstring> #define N 200005 using name ...
- leetcode.字符串.14最长公共前缀-Java
1. 具体题目 编写一个函数来查找字符串数组中的最长公共前缀.如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","fl ...
随机推荐
- DNS递归查询和迭代查询的差别
转载地址:http://blog.csdn.net/wuchuanpingstone/article/details/6720723 递归查询和迭代查询的差别 (1)递归查询 递归查询是一种DNS s ...
- 使用CNN做电影评论的负面检测——本质上感觉和ngram或者LSTM同,因为CNN里图像检测卷积一般是3x3,而文本分类的话是直接是一维的3、4、5
代码如下: from __future__ import division, print_function, absolute_import import tensorflow as tf impor ...
- 【HDU 2176】 取(m堆)石子游戏
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2176 [算法] Nim博弈 当石子数异或和不为0时,先手必胜,否则先手必败 设石子异或和为S 如果 ...
- maven 项目加载本地JAR
将jar安装到本地的maven仓库 1.首先确定本地有maven环境. 2.安装本地jar 模板: mvn install:install-file -Dfile=<path-to-file& ...
- 客户现场调试(连接oracle数据库)
1.System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本 http://blog.csdn.net/yucaoye/article/details/ ...
- Python中functools模块函数解析
Python自带的 functools 模块提供了一些常用的高阶函数,也就是用于处理其它函数的特殊函数.换言之,就是能使用该模块对可调用对象进行处理. functools模块函数概览 functool ...
- marge into操作
动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录 ...
- 安装wampserver遇到的问题及解决方案
丢失api-ms-win-crt-runtime-l1-1-0.dll 安装完wampserver,启动服务器的时候遇到一些问题,提示说缺失dll文件,如下图所示: 网上一搜,很多人出现过丢失api- ...
- HDU 3763 CD【二分查找】
解题思路:给出两个数列an,bn,求an和bn中相同元素的个数因为注意到n的取值是0到1000000,所以可以用二分查找来做,因为题目中给出的an,bn,已经是单调递增的,所以不用排序了,对于输入的每 ...
- ZOJ 3019 Puzzle
解题思路:给出两个数列an,bn,其中an,bn中元素的顺序可以任意改变,求an,bn的LCS 因为数列中的元素可以按任意顺序排列,所以只需要求出an,bn中的元素有多少个是相同的即可. 反思:一开始 ...