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 ( ...
随机推荐
- mybatis使用要点(2019.5.19)
接口入参 只有一个参数,叫啥都没问题 有两个参数以上,需使用@Param,否则名字依次为0.1.2和param1.param2.param3 一般用#,防sql注入:偶尔用$,比如需要动态表名等 接口 ...
- 条形码问题 dp+求某个序列在某种排列中的序号的方法
题目 条形码是一种由亮条(Light Bar)和暗条(Dark Bar)交替出现且以暗条为起头的符号,每条都占有若干个单位宽.图33-1给出了一个含有4个条的条形码,它延续了1+2+3+1=7单位的宽 ...
- Plugging an Unplugged Pluggable Database
1.unplug To unplug a PDB, you first close it and then generate an XML manifest file. The XML file co ...
- 用IARIdePm新建STM8工程步骤
IARdePm 如何新建工程及其调用库函数1.新建文件夹,例如,新建文件夹名字(不能为中文)为:Lib_test_GPIO_OUT2.新建工程,Create New Project...,选择Empt ...
- C. Molly's Chemicals 暴力 + 统计技巧
http://codeforces.com/contest/776/problem/C 一开始做的时候,就发现是预处理前缀和,然后对于每一个前缀和,如果他能成为一个贡献,就是能和前面的某些段 组合成和 ...
- DNS练习之正向解析
配置正向解析: 主机名:sishen63 主机IP:192.168.1.63 操作系统:Red Hat Enterprise Linux Server release 6.5 (Santiago) 安 ...
- Dapper系列之二:Dapper的事务查询
Dapepr讲解 上篇文章我们介绍了,什么是Dapepr,有什么好处,性能的对比,还有多表多数据添加操作(事务的封装)等等.本篇文章我们继续讲解.....如果本篇文章看不懂,请看我上一篇文章:Dape ...
- AJPFX谈JAVA新手问题之异常处理使用不当
★空的 catch 语句块 犯这种错误的人比较少,一般发生在刚学会 Java 或者刚参加工作不久的人身上. 所谓“空 catch 语句块”就是在 catch 语句块中没有对异常作任何处理(比如记错误日 ...
- Scrapy-Redis分布式爬虫小白问题记录
1.首先我是将Redis装在了阿里云的一台CentOS6.8上,使用ps -ef|grep redis查看是否成功运行 2.CentOS安装scrapy请参考 http://blog.csdn.net ...
- HYSBZ 1503 郁闷的出纳员 (Splay树)
题意: 作为一名出纳员,我的任务之一便是统计每位员工的工资.但是我们的老板反复无常,经常调整员工的工资.如果他心情好,就可能把每位员工的工资加上一个相同的量.反之,如果心情不好,就可能把他们的工资扣除 ...