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 ...
随机推荐
- deepin linux下python安装mysqldb
` sudo pip search MySQL-python `
- 分布式服务框架 Zookeeper(三)官方入门指南
入门指南:使用ZooKeeper来协调分布式应用 这篇文档包含了让你快速上手ZooKeeper的信息.主要是针对那些想要试一把ZooKeeper的开发人员,包含了安装一个单一ZooKeeper服务器的 ...
- Ecshop提示Only variables should be passed by reference in错误
Ecshop是个坑爹货,为什么tiandi会说它是个坑爹货呢,请看一下下面的官方的运行环境推荐: 服务器端运行环境推荐·php版本5.0以上5.3以下的版本(推荐使用5.2系列版本)·Mysql版本5 ...
- zeppelin部署
1.下载解压2.修改conf/zeppelin-env.sh,添加如下两行 export ZEPPELIN_PORT= export MASTER=spark://master:7077 3.启动 b ...
- jquery - 动态绑定事件
举个例子: html页面 <div><button type="button" class="test">测试</button&g ...
- 自动添加需要编译的源文件Android.mk模板
自动添加需要编译的源文件列表 添加第三方静态库.动态库的依赖 假设我们的项目依赖 libmath.a, libjson.a, libffmpeg.so 这几个第三方库文件,项目包含如下几个模块:a ...
- diamond源码阅读-diamond-server
diamond-server 1 增加一条数据 /diamond-server/admin.do?method=postConfig 1.1 调用 this.configService.addConf ...
- POJ 1426 Find The Multiple && 51nod 1109 01组成的N的倍数 (BFS + 同余模定理)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21436 Accepted: 877 ...
- ASP.NET动态网站制作(29)-- 正则
前言:继续讲框架,然后介绍正则的相关知识. 内容: 1.封装分页方法,方便以后调用:响应的CSS代码也可以封装. 2.WEB层里面的页面名称不要和model和dal里面的名称相同. 3.两个表联合查询 ...
- System、应用程序进程的Binder线程池和Handler消息循环
首先看一张Android系统启动流程图: