【LeetCode】LeetCode——第14题:Longest Common Prefix
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
题目的大概意思是:输入多个字符串,找到它们的最长公共前缀。
这道题难度等级:简单
说明:如:“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的更多相关文章
- Leetcode算法刷题:第14题 Longest Common Prefix
Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...
- leetcode第14题--Longest Common Prefix
Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- 【LeetCode算法-14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCode 14: Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
- LeetCodeOJ刷题之14【Longest Common Prefix】
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 【算法】LeetCode算法题-Longest Common Prefix
这是悦乐书的第146次更新,第148篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第5题(顺位题号是14),给定一个随机的字符串数组,查找这些字符串元素的公共前缀字符串, ...
- C# 写 LeetCode easy #14 Longest Common Prefix
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
随机推荐
- kvm虚拟机最佳实践系列2-创建KVM及KVM优化
创建KVM及KVM优化 把KVM优化与KVM创建放在一起,是因为我们创建的KVM是要用在生产环境中,所以基础优化工作是必备的. 创建KVM 创建系统盘, 大小: 操作系统通常都不到10G,所以系统盘2 ...
- RxVolley报错:Caused by: java.lang.RuntimeException: RequestQueue-> DiskBasedCache cache dir error
Caused by: java.lang.RuntimeException: RequestQueue-> DiskBasedCache cache dir error 这是因为SD卡动态权限导 ...
- centos7 挂载exfat格式的u盘
下载 两个包 rpm -ivh http://download1.rpmfusion.org/free/el/updates/7/x86_64/f/fuse-exfat-1.2.8-1.el7.x86 ...
- PHP成生若干位防伪码的方法
public static function createCode($length =16) { $arr = array_merge(range(0, 9), range('a', 'z')); / ...
- Logstash 最佳实践
https://doc.yonyoucloud.com/doc/logstash-best-practice-cn/index.html
- 状压DP【p1879】[USACO06NOV]玉米田Corn Fields
Description 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上 ...
- android利用adb修改手机的分辨率和dpi
在android开发过程中,适配更多的适配是必不可少的一步,而每次测试适配时,要么购买设配,要么模拟器,买设配太花钱,模拟器太占内存,不过幸好还可以通过修改手机的size(分辨率)和density来进 ...
- [Contest20180418]数学竞赛
题意:初始时$x=0$(长度),当$x$为长度时,你可以把$x$变成$\sin^{-1}x,\cos^{-1}x,\tan^{-1}x$之一($x$变为角度),若$x$为角度,你可以把$x$变成$\s ...
- Android中Context详解 ---- 你所不知道的Context(转)
Android中Context详解 ---- 你所不知道的Context(转) 本文出处 :http://b ...
- Chromium和Chrome的区别
1.Chromium是谷歌的开源项目,开发者们可以共同去改进它,然后谷歌会收集改进后的Chromium并发布改进后安装包.Chrome不是开源项目,谷歌会把Chromium的东西更新到Chrome中. ...