leetcode-algorithms-14 Longest Common Prefix

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

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

解法

从第一个字符串开始分别对其它字符串从头到尾匹配,只要碰到一个不相同的或已到其中一个字符串的末尾就说明其公共的字符串就是前面的i个字符.

class Solution
{
public:
string longestCommonPrefix(vector<string>& strs)
{
if (strs.size() <= 0) return ""; for (int i = 0; i < strs[0].length(); ++i)
{
for (int j = 1; j < strs.size(); ++j)
{
if (strs[j].length() == i || strs[0][i] != strs[j][i])
return strs[0].substr(0, i);
}
} return strs[0];
}
};

时间复杂度: O(n).

空间复杂度: O(1).

链接: leetcode-algorithms 目录

leetcode-algorithms-14 Longest Common Prefix的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  3. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  4. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

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

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

  6. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  7. 【LeetCode】14 - Longest Common Prefix

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

  8. 【LeetCode】14. Longest Common Prefix (2 solutions)

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  9. 《LeetBook》leetcode题解(14):Longest Common Prefix[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

随机推荐

  1. powerdesigner反转数据库的设计图

    PowerDesigner导入sql文件 1:点击 2:弹出该页面,点击确定 3:弹出一下页面,点击下面按钮选择文件 4:点击确定完成 二 .表转换为中文的执行脚本及步骤 执行脚本为:Option E ...

  2. BZOJ 1497: [NOI2006]最大获利(最大权闭合图)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 题意: 思路: 论文题,只要看过论文的话就是小菜一碟啦~ 每个用户群i作为一个结点分别向相应的 ...

  3. JCF

    点我 补两个红黑树原理:https://www.cnblogs.com/yyxt/p/4983967.html   https://www.cnblogs.com/skywang12345/p/324 ...

  4. java——File

    注意事项: 1:创建File对象需要导包, import java.io.File 2:File对象没有无参数构造.创建对象需要传参. 3:File类的对象,既可以代表文件也可以代表文件夹.   构造 ...

  5. 桂电第一次程序设计 C STL 排序

    Problem 1197 # 共享厕所 ediszhao添加于2017-12-22 15:00:56 时间限制 : 1000MS 内存限制 : 65536KB ×提示 : 你已解决此题目. 问题描述 ...

  6. iOS获取UUID

    转自:<iOS获取设备的唯一标识的方法总结以及最好的方法> 参考:<获取iOS设备唯一标识> 总结一下: 1.代码采用CFUUID+KeyChain的实现方式. 2.CFUUI ...

  7. iterm2 + zsh + oh-my-zsh +autojump

    iterm2  + zsh  +  oh-my-zsh +autojump 升级你的命令行工具,这个我折腾了好久,才明白了各个之间的关系. 1.先下载一个item2 首先你需要下一个iterm2,不要 ...

  8. 《UnityShader入门精要》学习笔记之渲染流水线

    第一种分类方式: 图形管道(如下7步): 顶点数据 : 由3D模型传递的三角形网格 顶点着色 : 编写CG程序对各个顶点进行着色 生成几何图元 : 连接特定的顶点生成几何图元,例如连接三个顶点生成一个 ...

  9. Python数据分析-Day2-Pandas模块

    1.pandas简介 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标 ...

  10. 多线程工具之CompletionService

    这里涉及到Java的多线程并发知识,以及线程池相关的知识.就不在此说明了.具体说说CompletionService的应用场景和使用方法. 比如我们有10个线程需要丢到线程池里面去执行,然后把10个线 ...