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. 解答: 注意 ...
随机推荐
- Relay张量集成
Relay张量集成 Introduction NVIDIA TensorRT是一个用于优化深度学习推理的库.这种集成将尽可能多地减轻从中继到TensorRT的算子,在NVIDIA GPU上提供性能提升 ...
- swagger 注解使用
@Api() 用于类:表示标识这个类是swagger的资源 tags–表示说明 value–也是说明,可以使用tags替代 但是tags如果有多个值,会生成多个list @ApiOperation() ...
- CentOS 6.x 安装图形界面
CentOS 6.x 安装图形界面一.首先查看系统的运行级别以及是否安装了桌面环境1.使用命令 runlevel 查看当前系统运行级别[root@42 ~]# runlevelN 32.使用命令 yu ...
- UF_EVAL 曲线或边分析
Open C UF_EVAL_ask_arc 圆形曲线或边分析,得到曲线或边的信息UF_EVAL_ask_ellipse 椭圆曲线或边分析,得到曲线或边的信息UF_EVAL_ask_hyperbo ...
- Selective Kernel Networks
摘要:在标准的卷积神经网络(CNNs)中,每一层的人工神经元的感受野被设计成具有相同的大小.众所周知,视觉皮层神经元的感受野大小受刺激的调节,但在构建cnn时却很少考虑到这一点.我们在神经网络中提出了 ...
- TCP/IP协议 (图解+秒懂+史上最全)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- 代码实测:给redis中的key取一个正确的名字多么重要
redis对写入的key长度有限制吗? 太长的key对性能有影响吗? key越长对性能影响越大? 如何评估键长度对性能的影响? talk is cheap, show me the code! 今天我 ...
- 关于MySql数据库误操作数据找回的办法
先讲个事,前段时间,系统长时间不用的一个功能被开放出来了,想当然的我没有在测试平台上测试,直接操作了正式系统(的确是我不严谨),导致好多数据异常,页面展示错乱了.于是我想到的第一个就是进行备份还原.项 ...
- Binding(四):数据校验
除了上一节讲的类型转换器,Binding还自带数据校验功能,这节主要来讲一下. 跟类型转换器一样,数据校验需要我们继承ValidationRule类,实现其中的Validate方法,并写入我 ...
- 10、修改windows编码集
10.1.查看Windows的字符集编码: 1.方法一: (1) 同时按住"windows"徽标键和"r"键,在弹出的"运行"框中输入&qu ...