题目

Write a function to find the longest common prefix string amongst an array of strings.

分析

该题目是求一个字符串容器中所有字符串的最长公共前缀。

AC代码

  1. class Solution {
  2. public:
  3. string longestCommonPrefix(vector<string>& strs) {
  4. if (strs.size() == 0)
  5. return "";
  6. else if (strs.size() == 1)
  7. return *(strs.begin());
  8. vector<string>::iterator beg, end;
  9. beg = strs.begin();
  10. //先得到前两个串的公共前缀
  11. string str = CommonPrefix(*beg, *(beg+1));
  12. //迭代器后移两个位置
  13. beg += 2;
  14. while (beg != strs.end())
  15. {
  16. if (str == "")
  17. break;
  18. str = CommonPrefix(str, *(beg++));
  19. }
  20. return str;
  21. }
  22. string CommonPrefix(const string &str1, const string &str2)
  23. {
  24. string common = "";
  25. if (str1 == "" || str2 == "")
  26. return common;
  27. int len1 = strlen(str1.c_str()) , len2 = strlen(str2.c_str());
  28. int len = len1 > len2 ? len2 : len1 ;
  29. for (int i = 0; i < len; i++)
  30. {
  31. if (str1[i] == str2[i])
  32. common += str1[i];
  33. else
  34. break;
  35. }
  36. return common;
  37. }
  38. };

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. 从输入url到浏览器显示页面的过程

    总体来说有两个大的方面: 一.网络通信连接部分.二.页面渲染展示部分. 细分详细过程: (网络通信) 1.输入url. 2.DNS解析域名. 3.拿到IP地址后,浏览器向服务器建立tcp连接. 4.浏 ...

  2. 如何用Python在10分钟内建立一个预测模型

    转载自:https://baijia.baidu.com/s?old_id=307995 最近,我从孙子(指<孙子兵法>——译者注)那里学到了一些策略:速度和准备 “兵之情主速,乘人之不及 ...

  3. Dima and Magic Guitar CodeForces - 366E

    Dima and Magic Guitar CodeForces - 366E 题意: http://blog.csdn.net/u011026968/article/details/38716425 ...

  4. 将picpick汉化及矩形截屏

  5. 浅谈Java中static作用--转

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...

  6. SonarQube+Svn+Jenkins环境搭建----问题总结

    1.配置SVN后提示unable to access to repository,原因是使用的账户没有访问svn的权限,创建新的用户即可.注意新的用户,用户名,密码要跟svn上的权限一致.     创 ...

  7. iOS之tableView性能优化/tableView滑动卡顿?

    本文围绕以下几点展开tableView性能优化的论述? 1.UITableViewCell重用机制? 2.tableView滑动为什么会卡顿? 3.优化方法? 4.总结 1.UITableViewCe ...

  8. 如何使用 Java 生成二维码

    步骤 下载jar包(QRCode.jar) maven项目手动引入jar包 编写实体类实现二维码的生成 controller调用 下载jar包(QRCode.jar) 下载网址如下: QRCode生成 ...

  9. CF782B The Meeting Place Cannot Be Changed

    题意: The main road in Bytecity is a straight line from south to north. Conveniently, there are coordi ...

  10. Android手机屏幕投射到电脑神器Vysor

    做android开发的,经常要把手机屏幕投射到电脑,用来演示.普遍的解决方案是360或者豌豆荚的演示功能,缺点是延迟非常厉害,大概有3秒左右,非常影响演示效果.以下介绍Vysor,几乎0延迟,能与手机 ...