题意:给多个字符串,返回这些字符串的最长公共前缀。

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

 class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans="";
if(strs.empty()) return ans;
int has[], j=;
while()
{
memset(has,,sizeof(has));
for(int i=; i<strs.size(); i++)
{
if(j==strs[i].size()) return ans;
has[strs[i][j]]++;
}
if(has[strs[][j]]!=strs.size()) return ans;
ans+=strs[][j++];
}
}
};

AC代码

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

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

  3. [LeetCode]14. Longest Common Prefix最长公共前缀

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

  4. 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 ...

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

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

  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 最长共同前缀

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

  8. 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. Sql调用WebService

    DECLARE @scid int,@rt int ) --创建MSSOAP.SoapClient组件(如果安装的是SoapToolkit30,应该是MSSOAP.SoapClient30,否则是MS ...

  2. POJ 3181 Dollar Dayz (完全背包,大数据运算)

    题意:给出两个数,n,m,问1~m中的数组成n,有多少种方法? 这题其实就相当于 UVA 674 Coin Change,求解一样 只不过数据很大,需要用到高精度运算... 后来还看了网上别人的解法, ...

  3. POJ 1496

    #include <iostream> #include <string> using namespace std; int fac(int num); int C(int n ...

  4. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)

    这里的高精度都是要去掉前导0的, 第一题:424 - Integer Inquiry UVA:http://uva.onlinejudge.org/index.php?option=com_onlin ...

  5. MY_Log,无缝替换原生Log,支持日志输出到文件、FirePHP

    自己扩展了一个MY_Log, 用法类似于log4j,目前支持将日志输出到文件.FirePHP.如果你需要将日志输出到其他地方,比如邮件.数据库等,可以很方便地进行扩展. 用法很简单,大家一看就知道.1 ...

  6. zoj 3591 Nim 博弈论

    思路:先生成序列再求异或,最多的可能为n*(n+1)/2: 在去掉其中必败的序列,也就是a[i]=a[j]之间的序列. 代码如下: #include<iostream> #include& ...

  7. C++函数默认参数

    C++中允许为函数提供默认参数,又名缺省参数. 使用默认参数时的注意事项: ① 有函数声明(原型)时,默认参数可以放在函数声明或者定义中,但只能放在二者之一 double sqrt(double f ...

  8. springMVC找不到JS等文件

     应用springMVC时 JS等文件找不到错误 应用springMVC时如果配置URL映射时如下配置 <servlet>        <servlet-name>appSe ...

  9. 306. Additive Number

    题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...

  10. C++:构造函数的默认参数知识拓展

    和普通函数一样,构造函数中参数的值既可以通过实参传递,也可以指定为某些默认值,即如果用户不指定实参值,编译系统就使形参取默认值.   例9.3的问题也可以使用包含默认参数的构造函数来处理.   [例9 ...