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

思路:直接逐个统计同一个位置上的字符有多少种,如果只有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. POJ 2566

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1445   Accepted: 487   Spec ...

  2. iOS视频压缩

    // // ViewController.m // iOS视频测试 // // Created by apple on 15/8/19. // Copyright (c) 2015年 tqh. All ...

  3. YAPF:Google开源的Python代码格式化工具

    点这里 现在的大多数 Python 代码格式化工具(比如:autopep8 和 pep8ify)是可以移除代码中的 lint 错误.这显然有些局限性.比如:遵循 PEP 8 指导的代码可能就不会被格式 ...

  4. C# 匿名方法 1027

    class Program { static void Main(string[] args) { SorAndShowFiles("Sorted by name", delega ...

  5. CodeIgniter API

    http://apigen.juzna.cz/doc/EllisLab/CodeIgniter/tree.html Classes CI_Benchmark CI_Calendar CI_Cart C ...

  6. poj 3613(经过N条边的最短路)

    题目链接:http://poj.org/problem?id=3613 思路:我们知道如果矩阵A表示经过1条边的方案数,那么A^N矩阵就代表这经过N条边的方案数,而本题中要求经过N条边的最短距离,于是 ...

  7. servlet中中文乱码问题

    在web项目中经常回碰到中文乱码的问题,特此整理一下,有不足的地方,希望大家纠正. 1从前台往后台传数据,.以get方式发送请求,发送的参数不乱,但是后台接收到参数乱码 在Tomcat的server. ...

  8. x11vnc

    http://cisight.com/how-to-setup-vnc-server-remote-desktop-in-ubuntu-11-10-oneiric/ Install VNC serve ...

  9. 一行代码设置TLabel.Caption的前世今生

    第零步,测试代码: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := 'Hello World'; end ...

  10. Visual studio C#语言输出调试信息到Output窗口方法

    1.菜单栏: 工具>选项>调试>将所有输出窗口文本重定向到即时窗口          2.使用Console.WriteLine或Write添加调试信息 3.按F5启动调试程序 4. ...