当前位置:网站首页>Longest common prefix (leetcode question 14)
Longest common prefix (leetcode question 14)
2022-07-07 19:14:00 【KUIND_】
subject
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string “”.
Example 1:
Input :strs = [“flower”,“flow”,“flight”]
Output :“fl”
Example 2:
Input :strs = [“dog”,“racecar”,“car”]
Output :“”
explain : Input does not have a common prefix .
Tips :
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] It's only made up of lowercase letters
Their thinking
The common idea is to get the first String Traverse the first character of to see whether it conforms to , If it meets the requirements, take out the first String The second character of
The idea of problem solving is
Find the first one String And the second String Public prefix of , Take this public prefix and the third string to take the public prefix , Traverse the last element of the trace in turn
Code
class Solution4 {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String prefix = strs[0];
int count = strs.length;
// Compare the two , Get the public prefix , Get the prefix and the next string to find the public prefix
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) {
// First find the minimum traversal length , Take the minimum length of two strings
int length = Math.min(str1.length(), str2.length());
int index = 0;
// Compare from left to right , success index++, If you fail, quit
while (index < length && str1.charAt(index) == str2.charAt(index)) {
index++;
}
// Returns the public prefix string
return str1.substring(0, index);
}
}
Test code
public static void main(String[] args) {
Solution4 solution4 = new Solution4();
String[] strs = {
"dog", "racecar", "car"};
System.out.println(solution4.longestCommonPrefix(strs));
}
边栏推荐
- Former richest man, addicted to farming
- 2022年推荐免费在线接收短信平台(国内、国外)
- POJ 1182: food chain (parallel search) [easy to understand]
- Will domestic software testing be biased
- Redis publishing and subscription
- Teach your sister to write the message queue hand in hand
- 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
- A hodgepodge of ICER knowledge points (attached with a large number of topics, which are constantly being updated)
- testing and SQA_ Dynamic white box test [easy to understand]
- Hutool - lightweight DB operation solution
猜你喜欢
企业展厅设计中常用的三种多媒体技术形式
5billion, another master fund was born in Fujian
基于图像和激光的多模态点云融合与视觉定位
Zhong Xuegao wants to remain innocent in the world
The live broadcast reservation channel is open! Unlock the secret of fast launching of audio and video applications
DeSci:去中心化科学是Web3.0的新趋势?
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
Desci: is decentralized science the new trend of Web3.0?
[tpm2.0 principle and Application guide] Chapter 9, 10 and 11
2022上半年朋友圈都在传的10本书,找到了
随机推荐
DeSci:去中心化科学是Web3.0的新趋势?
多个kubernetes集群如何实现共享同一个存储
Thread factory in thread pool
testing and SQA_动态白盒測试[通俗易懂]
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
Rules for filling in volunteers for college entrance examination
The live broadcast reservation channel is open! Unlock the secret of fast launching of audio and video applications
最长公共前缀(leetcode题14)
链式二叉树的基本操作(C语言实现)
數據驗證框架 Apache BVal 再使用
First time in China! The language AI strength of this Chinese enterprise is recognized as No.2 in the world! Second only to Google
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
国内首次!这家中国企业的语言AI实力被公认全球No.2!仅次于谷歌
企业展厅设计中常用的三种多媒体技术形式
伺服力矩控制模式下的力矩目标值(fTorque)计算
ip netns 命令(备忘)
PTA 1102 teaching Super Champion volume
如何给“不卖笔”的晨光估值?
LeetCode 890(C#)