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.
If there is no common prefix, return an empty string "".
1.2 中文题目
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
1.3输入输出
| 输入 | 输出 |
|---|---|
| strs = ["flower","flow","flight"] | "fl" |
| strs = ["dog","racecar","car"] | "" |
1.4 约束条件
- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] consists of only lower-case English letters.
2. 分析
2.1 一般方法
看到这道题,最简单的方法就是两层for循环,最外层是对第一个字符串进行遍历(其实第几个字符串都无所谓),内层是对字符数组进行遍历,意思也就是先看所有字符串第一个元素是否都一样,再看第二个,第三个,以此类推,当遇到有不同的时候,就可以跳出外层循环。代码如下:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans;
for (unsigned int i = 0; i < strs[0].size(); i++)
{
unsigned int count = 0;
for (unsigned int j = 0; j < strs.size(); j++)
{
char temp = strs[0][i];
if (i != strs[j].size() && strs[j][i] == temp)//
count++;
else
goto here;
if (count == strs.size())
ans += strs[0][i];
}
}
here:
return ans;
}
};
2.2 改进算法(贼好使)
上述算法使用goto跳出两层循环,而goto的使用很多人是不太推荐使用的,同时联想到for循环的第二项就是一个判断语句,因此可以将goto判断语句改到for循环里,具体代码如下:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans;
for (unsigned int i = 0; i < strs[0].size(); i++)
{
unsigned int count = 1;
unsigned int j = 1;
int temp = strs[0][i];
for (; j < strs.size() && i != strs[j].size() && strs[j][i] == temp; j++)
count++;
if (count == strs.size())
ans += strs[0][i];
else
break;
}
return ans;
}
};
这个算法时间消耗0ms,空间消耗9M,非常不错!
Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)的更多相关文章
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- [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 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- [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 最长公共前缀
公众号:爱写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. 思路:求最长前缀子 ...
- 【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- Longest Common Prefix -最长公共前缀
问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...
随机推荐
- camera数字降噪(DNR)
camera数字降噪(DNR) 闭路电视摄像机 无论多么出色和弱光,在黑暗中拍摄视频监控录像时都会不可避免地产生一些噪音.噪声是任何电子通信中不可避免的部分,无论是视频还是音频.本质上是静态的–视频信 ...
- 深度学习框架集成平台C++ Guide指南
深度学习框架集成平台C++ Guide指南 这个指南详细地介绍了神经网络C++的API,并介绍了许多不同的方法来处理模型. 提示 所有框架运行时接口都是相同的,因此本指南适用于所有受支持框架(包括Te ...
- UI自动化在RobotFramework中采用的分层设计
RF测试数据 RF测试数据由4种表数据组成.这些测试数据由表的第一个单元格标识,名称和用法如下: 表名 用法 别名 设置表 导入测试库,资源文件和变量文件.为测试套件和测试用例定义元数据 Settin ...
- 【NX二次开发】Block UI 微定位
属性说明 属性 类型 描述 常规 BlockID String 控件ID Enable Logical 是否可操作 Group ...
- noip2008 总结
noip 2008题解 笨小猴 原题 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大!这种方法的具体描述如下:假设 ...
- 入门Kubernetes - .Net Core 运行
前言: 之前文章 对Kubernetes 的一些基础概念及在windows下的环境搭建,接下来把.Net Core 运行到Kubernetes 中,在实际的操作中,对Kubernetes 的进一步学习 ...
- VsCode中添加tasks.json
选中项目文件夹,按ctrl+shift+p,输入tasks 选择之后,继续选择 然后选择 选中就可以了
- 微信小程序使用async await的一些技巧
在小程序onLoad事件中使用getItems(this) 和this.getItems() getItems(this)对应的方法为 this.getItems()对应的方法为 在getItems( ...
- 22、正则表达式(用于三剑客grep,awk,sed,内容中包含空行)
简单的说就是为处理大量的字符串而定义的一套规则和方法,通过定义特殊符号的辅助,系统管理员就可以快速过滤,替换城输出需要的字符串 : ^:^word 表示匹配以什么字符开头的内容: $:word$表示匹 ...
- 26、samba搭建
26.1.samba介绍: samba是一个网络服务器,基于linux操作系统,用于linux和windows之间数据的共享: Samba是一个能让Linux系统应用Microsoft网络通讯协议的软 ...