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. 【转发】Build Squid with SSL Bump and ICAP Client

    原文文档:http://docs.diladele.com/administrator_guide_3_4/installation_and_removal/filtering_https.html ...

  2. 如何禁用Eclipse的Validating

    使用Eclipse开发项目,在加载项目.刷新项目.修改了某个代码的时候,经常出现Eclipse正在Validating的提示.项目比较大文件(js)较多的情况下,甚至出现Validating几分钟的盛 ...

  3. CAN帧格式(标准帧、拓展帧)

    CAN2.0B标准帧 CAN 标准帧信息为11个字节,包括两部分:信息和数据部分.前3个字节为信息部分. 位置 7 6 5 4 3 2 1 0 字节01 FF RTR × × DLC(数据长度) 字节 ...

  4. OwnCloud 搭建

    owncloud 的安装方式可以在 [ 官网 ] 找到. 本文会说明 owncloud 在 CentOS 7.x 上基于 LAMP 安装, 以及简单的用 docker 镜像启动的例子. 基于 LAMP ...

  5. HDU1083_Courses_课程_C++

    给你们人工翻译一下题目哈,刚好练一下英语 对于一个组中有 N 个学生和 P 种课程.每个学生能够参加0种,1种或者更多的课程.你的任务是找到一种可能的方案使恰好P个学生同时满足下列条件: ‧ 方案中的 ...

  6. MFC does not support WINVER less than 0x0501 解决方案(转)

    原文转自 http://blog.csdn.net/ygzhong000/article/details/41750841 解决方案:在stdafx.h头文件中添加以下行. #define WINVE ...

  7. android hook 框架 ADBI 如何实现so注入

    Android so注入-libinject2 简介.编译.运行 Android so注入-libinject2  如何实现so注入 Android so注入-Libinject 如何实现so注入 A ...

  8. (二十四)linux新定时器:timefd及相关操作函数

    timerfd是Linux为用户程序提供的一个定时器接口.这个接口基于文件描述符,通过文件描述符的可读事件进行超时通知,所以能够被用于select/poll的应用场景. 一,相关操作函数 #inclu ...

  9. Mac下Lua环境搭建

    lua源文件下载安装 到官网安装了lua包,我安装的是 lua-5.3.1 解压之后,命令行cd进入到src目录下,输入make macosx 完成后cd ..到上一层目录, 输入sudo make ...

  10. Python学习杂记_11_函数(二)

    函数的高级运用这部分知识的核心内容就是你要把“函数即变量”这个概念理解并运用得出神入化... 一.函数的递归调用 所谓递归调用就是函数自己调用自己,在Python中如果不做限制递归调用的死循环最多可循 ...