当前位置:网站首页>Longest public prefix of leetcode
Longest public prefix of leetcode
2022-07-02 20:35:00 【wx58c8fa5d0b356】
List of articles
- Example
LeetCode The longest public prefix of the brush question
Title Description
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string “”.
Example
Example 1:
Input : [“flower”,“flow”,“flight”]
Output : “fl”
Example 2:
Input : [“dog”,“racecar”,“car”]
Output : “”
explain : Input does not have a common prefix .
explain :
All inputs contain only lowercase letters a-z .
Problem solving
Ideas
Take any character , from 0 Start to intercept string , See whether the interception result is the same as that of other strings .
If it's not the same , What I intercepted before is the longest public prefix .
Note that the characters you choose are not necessarily the shortest , To prevent the index from crossing the boundary , It needs to be verified .
Code
public static String longestCommonPrefix2(String[] strs) {
// Visibility verification
if (strs.length == 0) {
return "";
}
if (strs.length == 1) {
return strs[0];
}
// answer
String ans = "";
for (int i = 0; i < strs[0].length(); i++) {
String substring = strs[0].substring(0, i + 1);
for (int j = 1; j < strs.length; j++) {
if (substring.length() > strs[j].length()) {
return ans;
}
if (!substring.equals(strs[j].substring(0, i + 1))) {
return ans;
}
}
ans = substring;
}
return ans;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
A regular matching version is attached below , It's a little slow , It is not recommended to adopt
public static String longestCommonPrefix(String[] strs) {
// Shortest character
String shortest = strs[0];
for (int i = 1; i < strs.length; i++) {
if (strs[i].length() < shortest.length()) {
shortest = strs[i];
}
}
// One is "" The result is ""
if (shortest.length() == 0) {
return "";
}
for (int i = 0; i < shortest.length(); i++) {
String regex = "^" + shortest.substring(0, i + 1) + "\\w*$";
for (String element : strs) {
if (!Pattern.matches(regex, element)) {
return shortest.substring(0, i);
}
}
}
return shortest;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
边栏推荐
- How my mother-in-law and daughter-in-law get along
- Detailed upgrade process of AWS eks
- Research Report on the overall scale, major manufacturers, major regions, products and applications of micro hydraulic cylinders in the global market in 2022
- 【QT】QPushButton创建
- [QT] QPushButton creation
- 1005 spell it right (20 points) "PTA class a exercise"
- Spark source code compilation, cluster deployment and SBT development environment integration in idea
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of voltage source converters in the global market in 2022
- sql-labs
- API文档工具knife4j使用详解
猜你喜欢
Summary of interview experience, escort your offer, full of knowledge points
Cs5268 perfectly replaces ag9321mcq typec multi in one docking station solution
Review of the latest 2022 research on "deep learning methods for industrial defect detection"
c语言链表--待补充
Attack and defense world PWN question: Echo
Jetson XAVIER NX上ResUnet-TensorRT8.2速度与显存记录表(后续不断补充)
Resunet tensorrt8.2 speed and video memory record table on Jetson Xavier NX (continuously supplemented later)
测试人员如何做不漏测?这7点就够了
Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper
HDL design peripheral tools to reduce errors and help you take off!
随机推荐
Spark source code compilation, cluster deployment and SBT development environment integration in idea
Cron表达式(七子表达式)
Use graalvm native image to quickly expose jar code as a native shared library
C language linked list -- to be added
通信人的经典语录,第一条就扎心了……
This team with billions of data access and open source dreams is waiting for you to join
pytorch 模型保存的完整例子+pytorch 模型保存只保存可訓練參數嗎?是(+解决方案)
Don't you want to have a face-to-face communication with cloud native and open source experts? (including benefits
Send blessings on Lantern Festival | limited edition red envelope cover of audio and video is released!
【Hot100】21. Merge two ordered linked lists
How to realize the function of detecting browser type in Web System
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of multi-channel signal conditioners in the global market in 2022
API文档工具knife4j使用详解
2021 software security report: open source code, happiness and disaster depend on each other?
Resunnet - tensorrt8.2 Speed and Display record Sheet on Jetson Xavier NX (continuously supplemented)
Friends who firmly believe that human memory is stored in macromolecular substances, please take a look
Data preparation for behavior scorecard modeling
测试人员如何做不漏测?这7点就够了
勵志!大凉山小夥全獎直博!論文致謝看哭網友
Taiwan SSS Xinchuang sss1700 replaces cmmedia cm6533 24bit 96KHz USB audio codec chip