当前位置:网站首页>最长公共前缀(leetcode题14)
最长公共前缀(leetcode题14)
2022-07-07 17:05:00 【KUIND_】
题目
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
示例 1:
输入:strs = [“flower”,“flow”,“flight”]
输出:“fl”
示例 2:
输入:strs = [“dog”,“racecar”,“car”]
输出:“”
解释:输入不存在公共前缀。
提示:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] 仅由小写英文字母组成
解题思路
常见的思路是去拿第一个String的第一个字符遍历看是否符合,符合就取出第一个String的第二个字符进行遍历
题解的解题思路是
先找到第一个String和第二个String的公共前缀,拿这个公共前缀同第三个字符串取公共前缀,依次遍历道最后一个元素
代码
class Solution4 {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String prefix = strs[0];
int count = strs.length;
//两两进行比较,拿到公共的前缀,拿到前缀同下一个字符串求公共前缀
for (int i = 1; i < count; i++) {
prefix = longestCommonPrefix(prefix, strs[i]);
if (prefix.length() == 0) {
break;
}
}
return prefix;
}
public String longestCommonPrefix(String str1, String str2) {
//先求出最小的遍历长度,取两个字符串的最小长度
int length = Math.min(str1.length(), str2.length());
int index = 0;
//从左到右依次比较,成功index++,失败则退出
while (index < length && str1.charAt(index) == str2.charAt(index)) {
index++;
}
//返回公共前缀字符串
return str1.substring(0, index);
}
}
测试代码
public static void main(String[] args) {
Solution4 solution4 = new Solution4();
String[] strs = {
"dog", "racecar", "car"};
System.out.println(solution4.longestCommonPrefix(strs));
}
边栏推荐
- 脑洞从何而来?加州大学最新研究:有创造力的人神经连接会「抄近道」
- Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
- Version 2.0 of tapdata, the open source live data platform, has been released
- Cadre de validation des données Apache bval réutilisé
- 微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
- [software test] from the direct employment of the boss of the enterprise version, looking at the resume, there is a reason why you are not covered
- Will domestic software testing be biased
- Desci: is decentralized science the new trend of Web3.0?
- 50亿,福建又诞生一只母基金
- Redis
猜你喜欢
Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society
6. About JWT
Realize payment function in applet
【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的
Mathematical analysis_ Notes_ Chapter 11: Fourier series
从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
6.关于jwt
Nat address translation
直播预约通道开启!解锁音视频应用快速上线的秘诀
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
随机推荐
App capture of charles+postern
The top of slashdata developer tool is up to you!!!
[HDU] 5248 sequence transformation (greedy + dichotomy) [recommended collection]
Desci: is decentralized science the new trend of Web3.0?
嵌入式面试题(算法部分)
Static routing configuration
单臂路由和三层交换的简单配置
POJ 1182 :食物链(并查集)[通俗易懂]
Antisamy: a solution against XSS attack tutorial
SD_ DATA_ RECEIVE_ SHIFT_ REGISTER
Reuse of data validation framework Apache bval
线程池中的线程工厂
伺服力矩控制模式下的力矩目标值(fTorque)计算
反爬虫的重点:识别爬虫
The performance and efficiency of the model that can do three segmentation tasks at the same time is better than maskformer! Meta & UIUC proposes a general segmentation model with better performance t
2022.07.02
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
LeetCode 890(C#)
Nat address translation
How many times is PTA 1101 B than a