当前位置:网站首页>最长公共前缀(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));
}
边栏推荐
- Draw squares with Obama (Lua)
- In 2021, the national average salary was released. Have you reached the standard?
- How many times is PTA 1101 B than a
- 嵌入式面试题(算法部分)
- I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
- Do you know all four common cache modes?
- PTA 1101 B是A的多少倍
- Nat address translation
- [mime notes]
- Reuse of data validation framework Apache bval
猜你喜欢

Reinforcement learning - learning notes 8 | Q-learning

Standard ACL and extended ACL

2022.07.05

App capture of charles+postern

Micro service remote debug, nocalhost + rainbow micro service development second bullet

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

SlashData开发者工具榜首等你而定!!!

3.关于cookie

Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution

2022.07.02
随机推荐
数据验证框架 Apache BVal 再使用
Cadre de validation des données Apache bval réutilisé
ip netns 命令(备忘)
[Base64 notes] [suggestions collection]
PTA 1102 teaching Super Champion volume
Interview vipshop internship testing post, Tiktok internship testing post [true submission]
3.关于cookie
直播预约通道开启!解锁音视频应用快速上线的秘诀
如何选择合适的自动化测试工具?
Initial experience of cache and ehcache "suggestions collection"
The moveposition function of rigidbody2d of unity2d solves the problem of people or screen jitter when moving
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Numpy——axis
Draw squares with Obama (Lua)
二叉树的基本概念和性质
99% of people don't know that privatized deployment is also a permanently free instant messaging software!
Complete e-commerce system
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
单臂路由和三层交换的简单配置
抢占周杰伦