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. yum升级kernel

    # uname -a Linux host -.el6.x86_64 # SMP Fri May :: BST x86_64 x86_64 x86_64 GNU/Linux # cat /etc/re ...

  2. 使用java mail的网易smtp协议 发送邮件

    package com.enation.newtest; import java.security.GeneralSecurityException; import java.util.Propert ...

  3. FastDfs java客户端上传、删除文件

    #配置文件 connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 9090 http.an ...

  4. MySQL数据库从windows迁移到linux

    前几天搭建了lamp环境,想把之前写的小东西迁到linux上运行,涉及到把mysql数据库的文件迁移到linux上,直接用fileZilla传过去应该不行,我试了下,反正没成功.下面是我采用的方法: ...

  5. Hibernate的之间生成策略

    1.assigned 主键由外部程序负责生成,在save()之前必须指定一个.hibernate不负责维护主键生成.与hibernate和底层数据库都无关.在存储对象前,必须使用主键的setter方法 ...

  6. UVALIVE 3571 Visible Lattice Points

    就欧拉函数然后地推一下. #include <map> #include <set> #include <list> #include <cmath> ...

  7. 配置wpa_supplicant调试wifi linux下命令行连接wifi

    调试wpa_supplicant先读一下对应的README.txt文件.根据README.txt修改编译配置. wpa_supplicant需要打开以下开关进行编译CONFIG_DRIVER_NL80 ...

  8. 多个类的DLL封装及调用

    #define FaceLIBDLL #include "stdafx.h" #include "facedll.h" #include <opencv2 ...

  9. nvm: node版本管理工具

    安装nvm   curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash node 版本切 ...

  10. CentOS6.5升级autoconf版本,解决”Autoconf version 2.64 or higher is required“错误

    CentOS6.5升级autoconf版本,解决”Autoconf version 2.64 or higher is required“错误 https://blog.csdn.net/pretty ...