当前位置:网站首页>最长公共前缀(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));
}
边栏推荐
- 咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
- How many times is PTA 1101 B than a
- Where does brain hole come from? New research from the University of California: creative people's neural connections will "take shortcuts"
- Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
- 二叉树的基本概念和性质
- 从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
- SD_ DATA_ RECEIVE_ SHIFT_ REGISTER
- Redis的发布与订阅
- RIP和OSPF的区别和配置命令
- App capture of charles+postern
猜你喜欢

How to choose the appropriate automated testing tools?

RIP和OSPF的区别和配置命令

Charles+Postern的APP抓包

Differences between rip and OSPF and configuration commands

SlashData开发者工具榜首等你而定!!!
![[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](/img/73/cbbe82fd6bdfa8177f5bfcf683010d.jpg)
[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

Numpy——axis

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

Redis

Desci: is decentralized science the new trend of Web3.0?
随机推荐
Static routing configuration
Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
PTA 1101 B是A的多少倍
Simple configuration of single arm routing and layer 3 switching
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
10 schemes to ensure interface data security
Mathematical analysis_ Notes_ Chapter 11: Fourier series
The live broadcast reservation channel is open! Unlock the secret of fast launching of audio and video applications
初识缓存以及ehcache初体验「建议收藏」
Creative changes brought about by the yuan universe
PTA 1102 teaching Super Champion volume
App capture of charles+postern
In 2021, the national average salary was released. Have you reached the standard?
单臂路由和三层交换的简单配置
3.关于cookie
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
Rules for filling in volunteers for college entrance examination
In the first half of 2022, I found 10 books that have been passed around by my circle of friends
基于图像和激光的多模态点云融合与视觉定位