题目

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;
}
};

GitHub测试程序代码

LeetCode(14)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 ...

  2. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  3. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

  4. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  5. Leetcode算法刷题:第14题 Longest Common Prefix

    Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...

  6. LeetCode 14: Longest Common Prefix

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  7. 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 ...

  8. LeetCodeOJ刷题之14【Longest Common Prefix】

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  9. LeetCode (32) Longest Valid Parentheses

    题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

随机推荐

  1. the little schemer 笔记(5)

    第五章 “Oh My Gawd”:It's Full of Stars (rember* a l)是什么,其中a是cup,l是((coffee) cup ((tea) cup) rember*发音为r ...

  2. Python递归实现遍历目录

    import os filePath = "/Users/busensei/wzy/filePath/" def read(filePath, n): it = os.listdi ...

  3. hdu 4442 Physical Examination (2012年金华赛区现场赛A题)

    昨天模拟赛的时候坑了好久,刚开始感觉是dp,仔细一看数据范围太大. 题目大意:一个人要参加考试,一共有n个科目,每个科目都有一个相应的队列,完成这门科目的总时间为a+b*(前面已完成科目所花的总时间) ...

  4. Stars in Your Window POJ - 2482

    错误记录: 题目说输入在int范围内,但是运算过程中可能超int:后来开了很多longlong就过了 #include<cstdio> #include<algorithm> ...

  5. 题解报告:hdu 6441 Find Integer(费马大定理+智慧数)

    Problem Description people in USSS love math very much, and there is a famous math problem .give you ...

  6. 维骨力Glucosamine的最关键的几点...

    1.每日劑量應為多少?長期服用安全嗎? 由於葡萄糖胺(Glucosamine)和軟骨素(Chondroitin)原來就存在於人體,是人體每天會生產製造的必需營養素,因此,一般認為服用此類產品的安全性相 ...

  7. JavaScript入门2

    5.document对象:Document对象是window对象的一个对象属性,代表浏览器窗口中装载的整个HTML文档.文档中的每个HTML元素对应着JavaScript对象. 因为document代 ...

  8. 使用json传输数组实例

    client.php <?php //遍历数组元素,并将元素转码 function array_iconv($data, $in_charset='GBK', $out_charset='UTF ...

  9. aspx子集页面找父级页面元素

    var Obj= window.parent.document.getElementById("ctl00_RightTopTree_hidJsonResult"); Obj.va ...

  10. redis过期事件

    背景:目前在商城项目,订单有过期逻辑,小伙伴提议用redis做,经讨论分析,redis有key的过期事件,貌似可以实现,但是咨询大神,好像不建议这样用,可能会丢数据 随便写了段python代码测试 i ...