问题:链接

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

解答:

注意 当传入參数为空,即vector<string> 大小为0时。应该直接返回一个空字符串“”。而不是返回NULL。

这点须要特别注意。

代码:

class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.size() == 0)
return "";
int i = 0;
char a;
while(1)
{
if(i >= (*strs.begin()).size())
return strs[0].substr(0,i);
a = (*strs.begin())[i];
for(vector<string>::iterator it = strs.begin()+1; it != strs.end(); ++it)
{
if(i >= (*it).size() || a != (*it)[i] )
return strs[0].substr(0,i);
}
++i;
}
}
};

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 14:Longest Common Prefix 最长公共前缀

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

  3. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

  4. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  5. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  6. [leetcode]14. Longest Common Prefix 最长公共前缀

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

  7. 【LeetCode】Longest Common Prefix(最长公共前缀)

    这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...

  8. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  9. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

随机推荐

  1. Silverlight实例教程 - Validation客户端同步数据验证(转载)

    摘要:在Silverlight 4中,Silverlight Validation有相对的改进,本篇将介绍Silverlight 4中新加入的验证机制功能,IDataErrorInfo客户端同步验证机 ...

  2. 没有博士学位,照样玩转TensorFlow深度学习

    教程 | 没有博士学位,照样玩转TensorFlow深度学习 机器之心2017-01-24 12:32:22 程序设计 谷歌 操作系统 阅读(362)评论(0) 选自Codelabs 机器之心编译 参 ...

  3. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  4. Python内置函数之ascii()

    ascii()返回一个字符串对象. ascii()的参数只能有一个. 如果参数中有非ascii字符,会用 \u,\U,\x 来替代. ascii()和Python2中repr()等效 下面看看例子: ...

  5. codeblocks中给GCC编译器加参数

    在使用gcc命令行编译的时候可以使用gcc xxx.c -o xxx.exe -std=c99来使用c99标准编译 但是在codeblocks中默认是不使用c99标准编译的,如何加参数呢? Setti ...

  6. HTTP协议之http状态码详解

    什么是HTTP状态码 HTTP状态码的作用是:Web服务器用来告诉客户端,发生了什么事. 状态码位于HTTP Response 的第一行中,会返回一个”三位数字的状态码“和一个“状态消息”. ”三位数 ...

  7. LeetCode459. Repeated Substring Pattern

    Description Given a non-empty string check if it can be constructed by taking a substring of it and ...

  8. Xcode调试项目时取消弹出框提示授权

    问题2: instruments wants permission to analyze other processes.'DTServiceHub'需要控制另外一个进程,以便继续调试,键入密码以允许 ...

  9. 视频采集接口camera link 在8148中的应用

    (1)应用背景 (2)camera link 简介 (3)camera link 与8148 (4)camera link 应用实例 ---------------------author:pkf - ...

  10. redhat ent 6.5 virtualbox虚拟机通过桥接方式配置主机-虚拟机的局域网

    感谢: http://www.linuxidc.com/Linux/2012-06/62544.htm http://www.2cto.com/os/201204/126178.html Virual ...