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 ( ...
随机推荐
- LBP特征
此篇摘取 <LBP特征原理及代码实现> <LBP特征 学习笔记> 另可参考实现: <LBP特征学习及实现> <LBP特征的实现及LBP+SVM分类> & ...
- Codeforces Round #408 (Div. 2) D
Description Inzane finally found Zane with a lot of money to spare, so they together decided to esta ...
- AJPFX总结String类的特点
String str = "abc"; str就是String的一个对象 字符串一旦被赋值, 值就不能再被改变了 举例:String s ...
- html5新增的主题结构元素
article元素 article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容. 它可以是一篇博客或者报刊中的文章,一篇论坛帖子.一段用户评论或独立的插件. 或其他任何独立的 ...
- 自定义Toast的显示位置和显示内容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android手机app耗电量测试工具 - Gsam Battery Monitor
这段时间需要测试一个Android手机app的耗电量,在网上找了一个工具,Gsam Battery Monitor,觉得挺好用,和大家分享一下. 安装app后打开,可以看到主界面是这样的 点击一下上图 ...
- DLL入门浅析【转】
1.建立DLL动态库 动态链接库(DLL)是从C语言函数库和Pascal库单元的概念发展而来的.所有的C语言标准库函数都存放在某一函数库中.在链接应用程序的过程中,链接器从库文件中拷贝程序调用的函数 ...
- Linux 使用常见问题
1. 如何查看软件安装到什么位置 [Ubuntu] 今天安装了Lxc-docker,想看一下文件都安装到哪里了,首先找到这个包的ersion zhouh1@uhome:~$ dpkg -s lxc-d ...
- Delphi7中使用Indy9的IdSmtp发送email时subject过长会出现乱码的解决办法
procedure TIdMessageClient.SendHeader(AMsg: TIdMessage); var LHeaders: TIdHeaderList; begin LHeaders ...
- TensorFlow 安装 Win10 Python+GPU
前叙:有灵魂的程序都是每一个程序员的最终目标.TensorFlow了解下? 打算花几个月学机器学习,TensorFlow是很好的选择,折腾了会环境,略有心得分享下. 环境:win10 Python:3 ...