【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 ...
随机推荐
- TMS320F28379D 使用心得之 SCI
原文地址https://blog.csdn.net/qq_39545674/article/details/82597106 一.SCI 简介SCI(Serial Communication Inte ...
- 01深入理解C指针之---指针含义符号
该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 1.指针的含义: 指针本身也是变量,与其他一般变量不同的是:指针变量中没有存储具体类型 ...
- VIM使用技巧1
.命令是vim中很重要的一个命令,用法如下: 加入有一个文件vimtest.txt,内容如下: 1 Line one 2 Line two ...
- 让vim的在输入模式下现实光标不同
前几天用过苹果之后,发现vim中在插入模式下与命令模式下光标形状不同,根据光标形状就可以快速确认所在的模式,很方便,后来查了很多资料,一直查到官方的wiki也没有搞定,后来,终于搞定,现记录如下:我的 ...
- tomcat 异常:Caused by: org.apache.catalina.LifecycleException: The connector cannot start since the specified port value of [-1] is invalid
启动tomcat时出现异常: org.apache.catalina.LifecycleException: Failed to start component [Connector[AJP/1.3- ...
- 多线程之:竞态条件&临界区
竞态条件指:当一个对象或者一个不同步的共享状态,被两个或者两个以上的线程修改时,对访问顺序敏感,则会产生竞态条件. 临界区指:导致竞态条件发生的代码区. 如:increase块为临界区 public ...
- ws2s函数
std::string ws2s(const std::wstring& str) { char* pElementText; int iTextLen; // wide char to mu ...
- Ubuntu16.10 +python3.5+Tensorflow 1.1
1.python版本检查 因为Ubuntu16.10已经默认安装了python2.7 和 3.5,检查python版本, 如果为python2.7,那么就需要我们设置python3.5为默认版本. 查 ...
- 手机估值计算的jquery代码
<script type="text/javascript"> $('#inquiry').click(function(){ var total=0; var cou ...
- 路由器漏洞利用工具RouterSploit
路由器漏洞利用工具RouterSploit 网络中存在大量的嵌入式设备,如路由器.智能摄像头.这类设备安全防护程度较低.由于这些设备更新不方便,一旦发现漏洞,往往不能及时修复.所以,在网络渗透测试中 ...