当前位置:网站首页>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.
边栏推荐
- I would like to ask what securities dealers recommend? Is it safe to open a mobile account?
- 【Hot100】23. Merge K ascending linked lists
- The metamask method is used to obtain account information
- 【JS】获取hash模式下URL的搜索参数
- After 65 days of closure and control of the epidemic, my home office experience sharing | community essay solicitation
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of the inverted front fork of the global market in 2022
- Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper
- The first of the classic quotations of correspondents is heartbreaking
- API文档工具knife4j使用详解
- 疫情封控65天,我的居家办公心得分享 | 社区征文
猜你喜欢

B-end e-commerce - reverse order process

HDL design peripheral tools to reduce errors and help you take off!

Review of the latest 2022 research on "deep learning methods for industrial defect detection"

Basic concept of database, installation and configuration of database, basic use of MySQL, operation of database in the project

Resunet tensorrt8.2 speed and video memory record table on Jetson Xavier NX (continuously supplemented later)
![[fluent] dart technique (independent main function entry | nullable type determination | default value setting)](/img/cc/3e4ff5cb2237c0f2007c61db1c346d.jpg)
[fluent] dart technique (independent main function entry | nullable type determination | default value setting)

Why do I have a passion for process?
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/e9/f316d02c3cad226055dcdec6781436.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush

Complete example of pytorch model saving +does pytorch model saving only save trainable parameters? Yes (+ solution)

【Hot100】21. Merge two ordered linked lists
随机推荐
[Chongqing Guangdong education] reference materials for labor education of college students in Nanjing University
Outsourcing for three years, abandoned
【Hot100】21. Merge two ordered linked lists
Taiwan SSS Xinchuang sss1700 replaces cmmedia cm6533 24bit 96KHz USB audio codec chip
Attack and defense world PWN question: Echo
Cron表达式(七子表达式)
Friends who firmly believe that human memory is stored in macromolecular substances, please take a look
CS5268完美代替AG9321MCQ Typec多合一扩展坞方案
为什么我对流程情有独钟?
BitSet complement
JASMINER X4 1U deep disassembly reveals the secret behind high efficiency and power saving
【Hot100】23. 合并K个升序链表
JS modularization
Research Report on the overall scale, major manufacturers, major regions, products and applications of battery control units in the global market in 2022
「 工业缺陷检测深度学习方法」最新2022研究综述
Research Report on the overall scale, major manufacturers, major regions, products and applications of friction dampers in the global market in 2022
Driverless learning (4): Bayesian filtering
测试人员如何做不漏测?这7点就够了
Database schema notes - how to choose the right database in development + who invented relational database?
I did a craniotomy experiment: talk about macromolecule coding theory and Lao Wang's fallacy from corpus callosum and frontal leukotomy