14. Longest Common Prefix

My Submissions

Question
Editorial Solution
Total Accepted: 97052 Total
Submissions: 345681 Difficulty: Easy

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

Subscribe to see which companies asked this question

Show Tags

题目的大概意思是:输入多个字符串,找到它们的最长公共前缀。

这道题难度等级:简单

说明:如:“abcd”、“abefh”、“absyz”则其最长公共前缀为"ab";再如:“abcdefg”、“abcfg”、“gabcdest”则其最长公共前缀为空。通过举例说明可知,这里的最长公共前缀指的是从每一个字符串的第一个位置開始。若都同样,则匹配下一个。直到出现一个不同样或者某个字符串完结为止。

了解了题意之后。代码例如以下:

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

提交代码后。顺利AC,Runtime: 4
ms

【LeetCode】LeetCode——第14题:Longest Common Prefix的更多相关文章

  1. Leetcode算法刷题:第14题 Longest Common Prefix

    Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...

  2. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

  3. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  4. # Leetcode 14:Longest Common Prefix 最长公共前缀

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

  5. 【LeetCode算法-14】Longest Common Prefix

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

  6. LeetCode 14: Longest Common Prefix

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

  7. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...

  8. LeetCodeOJ刷题之14【Longest Common Prefix】

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

  9. 【算法】LeetCode算法题-Longest Common Prefix

    这是悦乐书的第146次更新,第148篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第5题(顺位题号是14),给定一个随机的字符串数组,查找这些字符串元素的公共前缀字符串, ...

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

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

随机推荐

  1. 在iOS上实现一个简单的日历控件

    http://blog.csdn.net/jasonblog/article/details/21977481 近期需要写一个交互有点DT的日历控件,具体交互细节这里略过不表. 不过再怎么复杂的控件, ...

  2. Linux getcwd()的实现【转】

    转自:http://www.cnblogs.com/alan-forever/p/3721908.html 通过getcwd()可以获取当前工作目录. 1 #include <unistd.h& ...

  3. Laravel Model Factory(模型工厂)的用法以及数据本地化

    Model Factory的位置 生成数据方法:make是生成数据,create是生成数据并保存到数据库 本地化方法 这样便生成了中文数据 整理自www.laravist.com视频教程

  4. Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. Python的程序结构[2] -> 类/Class[0] -> 类的特殊属性

    类的特殊属性 / Special Property of Class Python 中通过 class 进行类的定义,类可以实例化成实例并利用实例对方法进行调用. 类中还包含的一些共有的特殊属性. 特 ...

  6. LCA【bzoj3364】 [Usaco2004 Feb]Distance Queries 距离咨询

    Description  奶牛们拒绝跑马拉松,因为她们悠闲的生活无法承受约翰选择的如此长的赛道.因此约翰决心找一条更合理的赛道,他打算咨询你.此题的地图形式与前两题相同.但读入地图之后,会有K个问题. ...

  7. 有关奇葩的mex编程时的matlab出现栈内存错误的问题

    错误提示信息 (ntdll.dll) (MATLAB.exe中)处有未经处理的异常:0xC0000374:堆已损坏 该错误的表现是,matlab调用.mexw64函数时,第一次调用正常,第二次调用出现 ...

  8. onWebView检查网页中文

    问题:要检查网页中的一段文本: 开始我是这样写的: private final static String SPECIFIED_TEXT = "这个是一段中文"; onWebVie ...

  9. 关联模型中如果condition条件

    在练习中,有一个user表和地址表,一对多的关系. 我的想法是,通过这个关联模型找出这个用户下面默认值字段为1的地址 控制器中 public function index(){ $User = D(' ...

  10. win10 安装java

    https://jingyan.baidu.com/article/fea4511a12b158f7bb9125b9.html 一 下载java SE 官网 二设置环境变量 JAVA_HOME PAT ...