当前位置:网站首页>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));
}
边栏推荐
- [tpm2.0 principle and Application guide] Chapter 16, 17 and 18
- 我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
- DeSci:去中心化科学是Web3.0的新趋势?
- Nat address translation
- [Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question
- LeetCode 890(C#)
- 微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
- SD_ DATA_ SEND_ SHIFT_ REGISTER
- Charles+drony的APP抓包
- "Decryption" Huawei machine vision Corps: Huawei is moving up and the industry is moving forward
猜你喜欢
伺服力矩控制模式下的力矩目标值(fTorque)计算
Mathematical analysis_ Notes_ Chapter 11: Fourier series
面试唯品会实习测试岗、抖音实习测试岗【真实投稿】
如何选择合适的自动化测试工具?
[tpm2.0 principle and Application guide] Chapter 9, 10 and 11
Policy mode - unity
Comparison and selection of kubernetes Devops CD Tools
Continuous test (CT) practical experience sharing
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18
Creative changes brought about by the yuan universe
随机推荐
[Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question
Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
完整的电商系统
Review of network attack and defense
Comparison and selection of kubernetes Devops CD Tools
SlashData开发者工具榜首等你而定!!!
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
学习open62541 --- [67] 添加自定义Enum并显示名字
我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
"Decryption" Huawei machine vision Corps: Huawei is moving up and the industry is moving forward
2022.07.04
LeetCode 497(C#)
testing and SQA_ Dynamic white box test [easy to understand]
In the first half of 2022, I found 10 books that have been passed around by my circle of friends
Where does brain hole come from? New research from the University of California: creative people's neural connections will "take shortcuts"
Micro service remote debug, nocalhost + rainbow micro service development second bullet
手把手教姐姐写消息队列
Kirk borne's selection of learning resources this week [click the title to download directly]
Hutool - lightweight DB operation solution
Will domestic software testing be biased