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. VS2013 MFC C++ CString ,const char , char, string 类型转换

    VS2013 测试 以下测试加入头文件: # include <string>#include <cstdlib>using namespace std; //-------- ...

  2. linux日志服务之logwatch

    因为logwatch默认要使用sendmail服务,所以请参考linux之发送邮件--sendmail服务配置首先设置正确sendmail服务. 安装logwatch. 查看logwatch文件在/e ...

  3. centos6.5 网卡服务开机自动启动

    今天打开许久没用的centos之后,发现网络服务器不可用,通过service network restart 依然无法使用,简单记录一下处理过程: 1.通过setup 命令查看Network conf ...

  4. shell 练习 (免密钥登陆脚本)

    脚本说明 本地服务器ip 10.0.0.5 远程服务器地址 10.0.0.223 #!/bin/bashremote_ip=$ if [ ! -n "$1" ] ;then ech ...

  5. 2018 CCPC 女生专场

    可能是史上最弱的验题人—— Problem A (小)模拟. #include <bits/stdc++.h> using namespace std; int T; int main() ...

  6. Python的程序结构[1] -> 方法/Method[3] -> 魔术方法 __getattr__ 与代理模式

    __getattr__ 方法 __getattr__ 方法当对象调用内部属性(包括方法等)且未找到对应属性的时候会调用的特殊方法.利用这一特性,可是对函数实现一个代理模式. __getattr__方法 ...

  7. 差分【p3948】 数据结构

    顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述-->p3948 数据结构 分析 其实这题完全没有分析的 qwq. 只是因为写了差分数组相关知识,所以顺便写一下题解 qwq. 对于 ...

  8. Ext分区文件恢复工具extundelete

     Ext分区文件恢复工具extundelete Ext是延伸文件系统(Extended system)的缩写.它是为Linux内核开发的第一个文件系统.它有多个版本.现在常见的是Ext3和Ext4.由 ...

  9. 「CTSC2018」假面

    真~签到题qwq 昨天在考场上先写了个70分暴力dp,然后发现好像可以优化.因为结界技能的模型相当于要求出 对于每个物品,仅仅不选它的背包是什么....  于是当场脑补出两种做法: 前缀和后缀背包卷积 ...

  10. Dreamweaver CS5个性化设置

    Dreamweaver CS5个性化设置 编辑 --> 转到行 显示工具栏(上面包含“新建.保存.保存全部”等按钮) 查看 --> 工具栏 --> 标准 也可右键单击显示菜单 延伸阅 ...