当前位置:网站首页>最长公共前缀(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));
}
边栏推荐
- PTA 1102 teaching Super Champion volume
- 【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的
- Basic concepts and properties of binary tree
- [information security laws and regulations] review
- [mime notes]
- 学习open62541 --- [67] 添加自定义Enum并显示名字
- Simple configuration of single arm routing and layer 3 switching
- 【MIME笔记】
- Flipping Game(枚举)
- In 2021, the national average salary was released. Have you reached the standard?
猜你喜欢

Static routing configuration

Reuse of data validation framework Apache bval

Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products

Basic operation of chain binary tree (implemented in C language)

基于图像和激光的多模态点云融合与视觉定位

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

Standard ACL and extended ACL

NAT地址转换

Numpy——2.数组的形状

【塔望方法论】塔望3W消费战略 - U&A研究法
随机推荐
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18
嵌入式面试题(算法部分)
[mime notes]
我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
PTA 1101 B是A的多少倍
Comparison and selection of kubernetes Devops CD Tools
数据验证框架 Apache BVal 再使用
Reuse of data validation framework Apache bval
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
Realize payment function in applet
2022-07-04 matlab读取视频帧并保存
3.关于cookie
Initial experience of cache and ehcache "suggestions collection"
LeetCode 497(C#)
App capture of charles+drony
【剑指 Offer】59 - I. 滑动窗口的最大值
10 schemes to ensure interface data security
Desci: is decentralized science the new trend of Web3.0?