Longest Common Prefix

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

求最长公共前缀。

代码例如以下:

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int length = strs.size();
if (length <= 0)
return "";
string prefix = strs[0];
int i, j;
for (i=1; i<length; i++)
{
string tmpStr = strs[i];
if (prefix.length()==0 || tmpStr.length()==0)
return "";
int len = min(prefix.length(), tmpStr.length());
for (j=0; j<len; j++)
{
if (prefix[j] != tmpStr[j])
break;
}
prefix = prefix.substr(0,j);
}
return prefix;
}
};

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. 让JS帮你决定午餐吃什么吧

    最愁就是每天中午吃什么了,有空就做了个 JavaScript 轮播随机选择.会轮播预先自定义的菜单中,然后点选定的时候确定一款.代码可以查看本页源代码获得,你可以自定义修改菜单数组. 效果演示 准备选 ...

  2. js判断中出现两个!!是什么意思?

    在js中看源码时有时候出现两个!!,我起初以为是js的其他语法,其实!!就是两次取“非”的运算. 下面证明我的说法. alert(null);//false alert(!null);//true a ...

  3. bzoj 3309 DZY Loves Math 莫比乌斯反演

    DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1303  Solved: 819[Submit][Status][Dis ...

  4. eclipse 导出burpsuite插件包含第三方lib包

    第一步:右键项目点击export: 2.选择Runable JAR file: 点击Finish后会爆出一个错误(Jar export finished with problems. See deta ...

  5. http模拟登录

    = =其实很简单,写这个的目的呢,是为了练习下Ruby而已 就是post到登录地址,得到cookie,然后以后的请求都用这个cookie就好了. require 'net/http' module E ...

  6. xen 不同后端存储方案的性能对比

    概述 在xen平台下,一般使用文件来模拟一个磁盘.在xen中使用文件来模拟磁盘有3种方式, blkback 直接操作 blktap2 直接将文件映射为一个裸块设备,这样vm可以直接用phy的方式进行文 ...

  7. 【转】Talend作业设计模式和最佳实践-Part II

    转载地址:https://mp.weixin.qq.com/s?__biz=MzA3OTg1Mzk4Nw==&mid=2453261363&idx=1&sn=5674f1df8 ...

  8. Javascript短路表达式

    短路表达式:作为"&&"和"||"操作符的操作数表达式,这些表达式在进行求值时,只要最终的结果已经可以确定是真或假,求值过程便告终止,这称之为短 ...

  9. 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】

    题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...

  10. JQuery里面的下啦菜单

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...