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 ( ...
随机推荐
- autolayout UIImageView 根据 UILabel的宽度变换位置
仅个人学习笔记,大牛勿喷 代码写法 使用Masonry //昵称 _nameLableView = [[UILabel alloc]init]; [_nameLableView setTextColo ...
- nginx用户统计
1 概念 PV:页面访问量,即PageView,用户每次对网站的访问均被记录,用户对同一页面的多次访问,访问量累计. UV:独立访问用户数:即UniqueVisitor,访问网站的一台电脑客户端为一个 ...
- python操作pymongo
import pymongo from bson import ObjectId mongo_client = pymongo.MongoClient(host="127.0.0.1&quo ...
- 喵哈哈村的魔法考试 Round #1 (Div.2) ABCD
官方题解: http://www.cnblogs.com/qscqesze/p/6418555.html#3623453 喵哈哈村的魔法石 描述 传说喵哈哈村有三种神奇的魔法石:第一种魔法石叫做人铁石 ...
- 使用Appache部署WEB服务器
Apache的起源(这个就不说了,百度下就都有了) 简介:Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行, ...
- vue中的事件监听之——v-on vs .$on
跟着视频中老师的教学视频学vue的时候,看很多时候都用@(v-on)来监听子级emit的自定义事件,但在bus总线那块,又用.$on来监听bus自身emit的事件,v-on之间似乎相似但又不同,今天对 ...
- 【学习笔记】深入理解js原型和闭包(3)——prototype原型
既typeof之后的另一位老朋友! prototype也是我们的老朋友,即使不了解的人,也应该都听过它的大名.如果它还是您的新朋友,我估计您也是javascript的新朋友. 在咱们的第一节(深入理解 ...
- jsapi4加载的首个图层的范围被默认作为地图范围,且不能修改的解决
在map加载的第一个图层的图层范围(fullExtent),会被默认设置为map的全图范围,且不能更改,从一般地图控件角度来说,应该有fullExtent属性,作为地图的全图范围,但很遗憾jsapi4 ...
- chrome inspect出现白屏的解决方案
点inspect后 弹出框,可是里面一片白色 PS:原效果不是这样,只是图找不到随便p的 原因可以看这个:http://www.cnblogs.com/slmk/p/7591126.html 大概意思 ...
- Idea导入tomcat源码
1.下载资源 下载主要包含两个包,已经编译的包和源码包,如图所示. 链接地址为: http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.93/bin ...