LeetCode(14)Longest Common Prefix
题目
Write a function to find the longest common prefix string amongst an array of strings.
分析
该题目是求一个字符串容器中所有字符串的最长公共前缀。
AC代码
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.size() == 0)
return "";
else if (strs.size() == 1)
return *(strs.begin());
vector<string>::iterator beg, end;
beg = strs.begin();
//先得到前两个串的公共前缀
string str = CommonPrefix(*beg, *(beg+1));
//迭代器后移两个位置
beg += 2;
while (beg != strs.end())
{
if (str == "")
break;
str = CommonPrefix(str, *(beg++));
}
return str;
}
string CommonPrefix(const string &str1, const string &str2)
{
string common = "";
if (str1 == "" || str2 == "")
return common;
int len1 = strlen(str1.c_str()) , len2 = strlen(str2.c_str());
int len = len1 > len2 ? len2 : len1 ;
for (int i = 0; i < len; i++)
{
if (str1[i] == str2[i])
common += str1[i];
else
break;
}
return common;
}
};
LeetCode(14)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 OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- leetcode第14题--Longest Common Prefix
Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...
- # 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
Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...
- LeetCode 14: Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LeetCodeOJ刷题之14【Longest Common Prefix】
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- LeetCode (32) Longest Valid Parentheses
题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
随机推荐
- 从输入url到浏览器显示页面的过程
总体来说有两个大的方面: 一.网络通信连接部分.二.页面渲染展示部分. 细分详细过程: (网络通信) 1.输入url. 2.DNS解析域名. 3.拿到IP地址后,浏览器向服务器建立tcp连接. 4.浏 ...
- 如何用Python在10分钟内建立一个预测模型
转载自:https://baijia.baidu.com/s?old_id=307995 最近,我从孙子(指<孙子兵法>——译者注)那里学到了一些策略:速度和准备 “兵之情主速,乘人之不及 ...
- Dima and Magic Guitar CodeForces - 366E
Dima and Magic Guitar CodeForces - 366E 题意: http://blog.csdn.net/u011026968/article/details/38716425 ...
- 将picpick汉化及矩形截屏
- 浅谈Java中static作用--转
static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...
- SonarQube+Svn+Jenkins环境搭建----问题总结
1.配置SVN后提示unable to access to repository,原因是使用的账户没有访问svn的权限,创建新的用户即可.注意新的用户,用户名,密码要跟svn上的权限一致. 创 ...
- iOS之tableView性能优化/tableView滑动卡顿?
本文围绕以下几点展开tableView性能优化的论述? 1.UITableViewCell重用机制? 2.tableView滑动为什么会卡顿? 3.优化方法? 4.总结 1.UITableViewCe ...
- 如何使用 Java 生成二维码
步骤 下载jar包(QRCode.jar) maven项目手动引入jar包 编写实体类实现二维码的生成 controller调用 下载jar包(QRCode.jar) 下载网址如下: QRCode生成 ...
- CF782B The Meeting Place Cannot Be Changed
题意: The main road in Bytecity is a straight line from south to north. Conveniently, there are coordi ...
- Android手机屏幕投射到电脑神器Vysor
做android开发的,经常要把手机屏幕投射到电脑,用来演示.普遍的解决方案是360或者豌豆荚的演示功能,缺点是延迟非常厉害,大概有3秒左右,非常影响演示效果.以下介绍Vysor,几乎0延迟,能与手机 ...