Longest Common Prefix -最长公共前缀
问题:链接
Write a function to find the longest common prefix string amongst an array of strings.
解答:
注意 当传入參数为空,即vector<string> 大小为0时。应该直接返回一个空字符串“”。而不是返回NULL。
这点须要特别注意。
代码:
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.size() == 0)
return "";
int i = 0;
char a;
while(1)
{
if(i >= (*strs.begin()).size())
return strs[0].substr(0,i);
a = (*strs.begin())[i];
for(vector<string>::iterator it = strs.begin()+1; it != strs.end(); ++it)
{
if(i >= (*it).size() || a != (*it)[i] )
return strs[0].substr(0,i);
}
++i;
}
}
};
Longest Common Prefix -最长公共前缀的更多相关文章
- [LeetCode]14. Longest Common Prefix最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- 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 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- [leetcode]14. Longest Common Prefix 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- [LeetCode] 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. If there is n ...
随机推荐
- [DevExpress]XtraTabControl右键加入关闭当前页、关闭其它页、所有关闭的实现
private void xtraTabControl_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButton ...
- JanusGraph与Cassandra集成模式
//如果使用的是cassandra 2.2或更高版本,需要开启thift,以使janus连接到cassandra. ./bin/nodetool enablethrift. 15.1 Local Se ...
- silverlight RadGridView总结三(转载)
在RadGridView中进行分组以及导出 分组 主要是在前台进行分组的定义: 前台代码: <telerik:RadGridView x:Name="RadGridView1" ...
- python学习之函数返回值
python中函数返回值的方式有2种: 1.return语句 说明:return语句执行完后,函数后续的代码将不会被执行 2.yield语句 说明:yield语句返回的是一个迭代器对象,可以通过nex ...
- CentOS设置sendmail发件人,让sendmail不显示通过XXX代发
0.有一个十分快速的方法 命令:hostname itzhanzhang.com,但是重启后会失效,于是请接着往下看一劳永逸的方法: 1.设置你的主机名 默认的主机名是类似于“VM_24_76_cen ...
- 树莓派、 Arduino 、传统单片机开发板该如何选择?
几十年前的电子爱好者,最喜欢的就是电烙铁.面包板和收音机:十几年前,出现了单片机,于是玩具就成了电烙铁.面包板和单片机:到了2015年,贴片技术的不断普及,让面包板不再那么有用武之地,经济的发展也让现 ...
- java gc日志详解
从 Full GC 信息可知,新生代可用的内存大小约为 18M,则新生代实际分配得到的内存空间约为 20M(为什么是 20M? 请继续看下面...).老年代分得的内存大小约为 42M,堆的可用内存的大 ...
- wp8页面导向
一般打开的是MainPage.xaml需要打开另一个页面的时候,用NavigationService.Navigate(uri);当然uri要配置是相对路径还是绝对路径Uri uri = new Ur ...
- PHP编译configure时常见错误
PHP的安装虽然有时候很简单,可是如果应用一多,我们安装起来就很头痛了!出错最多的就是安装PHP扩展的时候了.其实不管是你是Apache类的应用还是Nginx类的,PHP的安装都不是很简单,虽然网上有 ...
- HashMap与HashTable原理及数据结构
HashMap与HashTable原理及数据结构 hash表结构个人理解 hash表结构,以计算出的hashcode或者在hashcode基础上加工一个hash值,再通过一个散列算法 获取到对应的数组 ...