LeetCode之LCP(Longest Common Prefix)问题
这个也是简单题目。可是关键在于题意的理解。
题目原文就一句话:Write a function to find the longest common prefix string amongst an array of strings.
题意是给一个字符串数组,找出这个字符串数组中全部字符串的最长公共前缀。
注意是限定的前缀。而不是子串。
所以,直接的解法就是以第一个字符串为基准,逐个比較每一个字符。算法复杂度也显然是O(M*N)。M是字符串的个数,N是最长前缀的长度。
代码例如以下:
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
int len = strs.size();
string prefix;
if(len == 0)return res;
for(int pos = 0; pos < strs[0].size(); pos++)//最长前缀的长度不超过strs[0].size(),逐个字符的比較
{
// 各个字符串相应位置的字符比較
for(int k = 1; k < len; k++)
{
if(strs[k].size() == pos || strs[k][pos] != strs[0][pos])
return res;
}
prefix.push_back(strs[0][pos]);
}
return prefix;
}
};
LeetCode之LCP(Longest Common Prefix)问题的更多相关文章
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 【LeetCode】14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...
- 【LeetCode】14. Longest Common Prefix (2 solutions)
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 《LeetBook》leetcode题解(14):Longest Common Prefix[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:Longest Common Prefix(最长公共前缀)
Write a function to find the longest common prefix string amongst an array of strings. 求很多string的公共前 ...
随机推荐
- 关于vector.size()和string.length() 的返回类型 size_type
今天写循环的时候碰到一个问题,发现:string.length()返回的类型是size_type.它是unsigned 类型.string::size_type它在不同的机器上,长度是可以不同的,并非 ...
- cocos2d-x 不规则碰撞检测 【转载】
原文:http://www.2cto.com/kf/201401/272331.html //判断有没有点到有材质的部分, p_point相对, CCSprite坐标 (p_point是相对 Spr ...
- MYSQL创建用户和授权方法(测试mysql5.7)
一.创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...
- [Windows Server 2012] Tomcat安装方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:Win20 ...
- HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- Redis 之仿微博demo
一.用户注册登录 include './header.php'; include './function.php'; $username = p('username'); $password = p( ...
- SpringMVC知识点总结一(非注解方式的处理器与映射器配置方法)
一.SpringMVC处理请求原理图(参见以前博客) 1. 用户发送请求至前端控制器DispatcherServlet 2. DispatcherServlet收到请求调用HandlerMappi ...
- js取自定义data属性
//20170329 原本以为只能attr或者prop来获取属性,但是今天看别人的代码他自定义了一个属性,却取不到他的属性值,我自己在本地又可以取到,难道是phtml的原因,于是我到网上查找,发现了一 ...
- 为什么Java中的密码优先使用 char[] 而不是String?
可以看下壁虎的回答:https://www.zhihu.com/question/36734157 String是常量(即创建之后就无法更改),会保存到常量池中,如果有其他进程可以dump这个进程的内 ...
- 关于img的一个小知识点
这两天在撸代码的时候发现图片的最下面总是会留一条空白,刚开始不知道为什么,但是UI拿刀对我说去掉它,瑟瑟发抖的我找了下原因及解决方案. 原因:img 是一个inline-block标签,而这个标签的v ...