Longest Common Prefix

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

求最长公共前缀。

代码例如以下:

  1. class Solution {
  2. public:
  3. string longestCommonPrefix(vector<string>& strs) {
  4. int length = strs.size();
  5. if (length <= 0)
  6. return "";
  7. string prefix = strs[0];
  8. int i, j;
  9. for (i=1; i<length; i++)
  10. {
  11. string tmpStr = strs[i];
  12. if (prefix.length()==0 || tmpStr.length()==0)
  13. return "";
  14. int len = min(prefix.length(), tmpStr.length());
  15. for (j=0; j<len; j++)
  16. {
  17. if (prefix[j] != tmpStr[j])
  18. break;
  19. }
  20. prefix = prefix.substr(0,j);
  21. }
  22. return prefix;
  23. }
  24. };

LeetCode 14: Longest Common Prefix的更多相关文章

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

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

  2. LeetCode OJ:Longest Common Prefix(最长公共前缀)

    Write a function to find the longest common prefix string amongst an array of strings. 求很多string的公共前 ...

  3. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  4. 【LeetCode】LeetCode——第14题:Longest Common Prefix

    14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...

  5. LeetCode第[14]题(Java): Longest Common Prefix

    题目:最长公共前缀 难度:EASY 题目内容: Write a function to find the longest common prefix string amongst an array o ...

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

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

  7. 【LeetCode OJ 14】Longest Common Prefix

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

  8. 【LeetCode算法-14】Longest Common Prefix

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

  9. leetcode第14题--Longest Common Prefix

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

随机推荐

  1. [原]C++拾遗

    int a=3,b=4; bool ok=(a==2,b==4); printf("%d\n",ok); //输出的结果是1,逗号既不是&& 也不是|| 应该是从前 ...

  2. SHH框架的搭建

    建立一个Web项目,然后导入如下包 l  struts2包:在struts2-blank.war下的lib目录下,以及struts-2.3.15.1\lib下的struts和spring整合的插件包s ...

  3. SJCL:斯坦福大学JS加密库

    斯坦福大学Javascript加密库简称SJCL,是一个由斯坦福大学计算机安全实验室创立的项目,旨在创建一个安全.快速.短小精悍.易使用.跨浏览器的JavaScript加密库.(斯坦福大学下载地址:h ...

  4. poj2728 最小比率生成树——01分数规划

    题目大意: 有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水, 只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差, 现在要求方案使得费用与距离的比值最小,很显然 ...

  5. [置顶] Linux 虚拟地址与物理地址的映射关系分析【转】

    转自:http://blog.csdn.net/ordeder/article/details/41630945 版权声明:本文为博主(http://blog.csdn.net/ordeder)原创文 ...

  6. CentOS系统最小化安装没有wget解决方案

    -bash: wget: command not found的两种解决方法 今天给服务器安装新LNMP环境时,wget 时提示 -bash:wget command not found,很明显没有安装 ...

  7. 源码安装cmake(或者叫升级cmake)

    cmake source install as follows: 0 cd ~ 1 wget https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz 2 tar ...

  8. HDU 2586.How far away ?-在线LCA(ST)-代码很认真的写了注释(捞到变形)

    2018.9.10 0:40 重新敲一遍,然后很认真的写了注释,方便自己和队友看,刚过去的一天的下午打网络赛有一题用到了这个,但是没写注释,队友改板子有点伤,因为我没注释... 以后写博客,代码要写注 ...

  9. Python的并发并行[1] -> 线程[0] -> threading 模块

    threading模块 / threading Module 1 常量 / Constants Pass 2 函数 / Function 2.1 setprofile()函数 函数调用: thread ...

  10. #421 Div1 C

    #421 Div1 C 题意 在 (0, n) 和 (m, 0) 处各有一个装置,从起始点(0, 0)出发,首先走短路到 (m, 0) 拿起装置回到起始点,再去 (0, n) 处拿起装置回到起始点.当 ...